Lesson 1.3 – Dissecting the First Application you Wrote in C#
C# Course for Absolute Beginners – Lesson 1.3
Next Lesson
Introduction
This lab will start the process of dissecting the first application that was created in the previous lab. This lab will review every line of code in the program.CS file that was just created, as a basis for further exploration in C#.
To review:
The previous lab created code in an area between two curly braces that was executed whenever the application was run. The code accomplished two things:
-It printed a text statement in the Windows console
-It caused the application to pause for just a moment, waiting for input before it would continue on.
This lab will start the discussion from the inside of that code block and work outward. There’s a lot of text, and it’s important to understand at this early stage what all those words and all those symbols actually accomplish.
BONUS: Watch the Video for this Lesson!
Check out the other FREE videos here …
Step 1: Understanding the Keywords Console, the .NET Framework Runtime, Classes and Methods.

Take a look at the word ‘Console’ which is the very first thing that was typed in. What is this word Console?
When learning how to write applications with C#, learning the syntax of C#, or rather the nouns and the verbs and the punctuation of programming language, is half the battle. The other half is learning the .NET Framework. The.NET Framework is like a protective bubble that an application lives inside of. When code is written, the .NET Framework makes the programmer’s job a lot easier in a couple of very important ways.
First of all, the code runs inside of the .NET Framework Runtime, which is an application that wraps around your application. The Runtime takes care of low-level details – it manages how the data in an application is moved into and out of memory. It provides protection from accidentally crashing someone else’s computer, and it also provides a library of functionality. So code can make use of a library of code that has been written by Microsoft to take care of low-level details like displaying data to the screen, moving data from one computer to another, working with dates and times, or common tasks like opening files, reading and writing to them, and so on. As a result programmers can focus on the important elements, the real value, or the purpose of an application.
This library of functionality is called the .NET Framework Class Library, or the FCL. Some books refer to it as the Base Class Library or the BCL. It’s the exact same thing – it’s that library of code that Microsoft makes available. This lab will focus on the .NET Framework Class Library. When the word Console.WriteLine was typed into the application, we were using some code in the library that knows how to write a line of text to the Windows Console. All that the programmer has to do is tell the program what text should actually be displayed and the programmer can focus on the important part. The programmer isn’t concerned with how the writes the text; only that the text actually is displayed into the window.
The Console part of that Console.WriteLine is called the class. The WriteLine part is called a method. The method ‘WriteLine’ is the part that does all the hard work. For right now the console part can be thought of as as a way of organizing code. Soon the definition of what a class is will be expanded, but for the moment this will serve as a working definition. The console class has another method called ‘ReadLine’ and Microsoft has gathered all the code together that deals with the Windows Console and organizes them into a class called Console. Later labs will demonstrate how to write custom classes, methods and libraries to organize code. Remember – this definition will be expanded to provide a broader definition in the future.
The WriteLine method allows a programmer to write to the Console Window, and is called a Method Parameter. Some methods accept no parameters. For example in the Console.ReadLine, no parameter was sent into that method. However, with WriteLine we told it we wanted it to write the string ‘hello world’ into the Console Window. So a parameter was passed. Some methods can accept a virtually infinite number of parameters; these will be discussed later. In the case of WriteLine, we wrote the argument "hello world" between parameters. Between the ReadLine method there is an open and closed parenthesis with nothing between. That means it doesn’t accept any parameters.
Step 2: The Importance of Double Quotation Marks in String Literals

Notice that when the words "Hello World" were passed in, they were surrounded with double quotation marks. To understand the importance of the double quotation marks, delete them. Notice what happens: Red squiggly lines appear underneath of both words.
Step 3: Displaying Error Messages with Missing Quotation Marks

When the application is run an error is thrown, and if we take a look at the error list it would show a number of problems. The double quotes tell C# that a Literal String is being passing in. In other words, literally those exact words are to be displayed. Since the program doesn’t understand what the application is trying to do without the quotation marks, the program tells us that there is a problem via those red squiggly lines and the error messages in the error list.
Replace the quotation marks now.
Step 4: Understanding the Main Method

Let’s continue our discussion of methods by looking at another method: the Main method. In this case, this is where the code is written. Notice the ‘static void Main.’ Here a method is called. The application is defining what goes inside of a method. A method is a block of code that has a name. The squiggly braces define a code block. In this case they demonstrate containment, meaning that the code block owns the sub code block. Whatever’s inside the first owns the second. The code block is given a name called ‘main.’ Since it has a name, it can be called.
In the case of a simple console application, by default the main method is the first method that’s executed. Notice that this Main method defines a parameter. Here a parameter is passed to a method that’s already been defined in the Framework Class Library. A parameter is defined for main. This will be discussed in-depth in another lab. It will make a lot more sense later. For now, understand that a method is a named block of code, and for today’s purposes think of a class as a container for methods. Again, this definition will be expanded soon.
Step 5: Understanding the Relationship Between Methods, Classes and Namespaces

(1) There is a class defined, called Program.
(2) Outside of that there is something called ‘namespace Day1’
It’s important to understand the relationship between a method, a class and a namespace. The namespace part that is highlighted is like a last name, or surname, for classes. It allows two classes with the exact same name, without creating any conflict from the .NET Framework’s point of view. It may not make a lot of sense now why these even exist, but as your applications grow from just a couple lines of code to hundreds or thousands of lines of code, this will make perfect sense.
For now think of name spaces like this: If we were to change the name space to my last name (Tabor) and then change the class name to my first name (Bob), you could do something like this:
-Bob, run your main method
-I might ask: "What Bob are you talking to? There’s a ton of Bob’s in the world"
-You would reply, "I’m speaking Tabor.Bob."
Name spaces help avoid ambiguity. We’re not just talking to any Bob, we’re talking to Tabor.Bob. That narrows it down dramatically. Whenever classes are referenced in both the .NET Framework class library as well as libraries that are custom created, confusion and ambiguity can be avoided by defining namespaces, or giving classes a last name.
Step 6: Understanding Using Statements and Their Relationships to Namespaces

The same is true of the console class. Only Console is used. However Console has a last name, called System. The full name is used here:
(1) System.Console.Writeline and then System.Console.ReadLine.
You may be wondering, "Why don’t you have to type in System?"
(2) The reason is because of the using statement at the very top, ‘using System’’ That means that a programmer doesn’t have to type the namespace. This is the equivalent of saying, "Whenever you see Bob, just assume it’s Tabor.Bob, or rather whenever you see Console, just assume that we’re talking about System.Console.
The purpose of this is to save a lot of typing for the developer – it’s a convenience. Whenever a new project is created, the IDE automatically added some common name spaces as using statements. These can be removed by highlighting them and hitting "Delete" on the keyboard, since many of them aren’t used. Everything would still work perfectly. Again, this is just a convenience or a shortcut. As you get more experienced you’ll probably want to keep that list trimmed down to avoid ambiguity. It’s good to keep that list as small as possible.
Step 7: Using Semicolons to end Programming Commands; The Importance of Code Readability

Notice the semicolon at the very end of certain lines of code that we’ve written – for example, the end of the Console.WriteLine. It exists in a couple of different places throughout the application, including the using statements and the lines of code that we wrote.
The semicolon is similar to the period at the end of an English sentence. It completes the thought. Some programming languages, like Visual Basic, only allow one thought per line of code and don’t really need any punctuation at the end of the sentence. However with C# a developer can organize multiple complete thoughts on a single line of code. Most programmers never do, but could and the code would work just fine. A single thought can also be broken up into multiple lines of code. It may look kind of strange isn’t done frequently. However there are times when it may be useful – Typically when writing long code, and ensuring that it doesn’t extend off the viewable area for coworkers who might be working on the same project. You may have a very large screen. They may have a very small screen. So you might want to organize your code and write it in such a way that it all fits nicely. There are some rules and some idioms around how to best structure code and make it readable in that regard.
The line breaks and the whitespace don’t matter to C# at all. It’s only the semicolons that matter. Notice also that there is some intense formatting. There are four indentation levels before the code is actually written. This is all completely optional. A developer isn’t required to have any indentation as far as the C# compiler or the .NET Runtime is concerned, but it’s important for the sake of readability. Visual C# Express Edition adds the indentation by default. This is important because reading other people’s code, and even reading your own code at times, can be difficult. Any kind of visual clues or hints to help break down and understand what’s being expressed is very useful.
Also notice that the IDE uses color coding for certain text that’s been typed into the code window. In this case, class names like Program and Console are a greenish color. Special C# reserved words are in a very bright blue, and String Literals are in red. This makes code more human readable. It has no benefit whatsoever for the C# compiler or the .NET Framework Class Library, or the .NET Framework Runtime.
Conclusion
To review what was discussed in this lab:
(1) A method is a named block of code in C#.
(2) A block of code is defined with curly braces.
(3) A method can accept parameters as a means of sending information into the method for processing. The words ‘hello world’, a literal string of characters, were sent into the WriteLine method for processing. In this case it used that literal string that we passed it to be displayed on-screen.
(4) Parameters are accessed using an open and closed parenthesis.
(5) Microsoft created the .NET Framework Class Library, which contains thousands of methods that we can call by name to perform various low-level tasks. This allows a developer to focus on the important functionality within applications.
(6) Classes are a means of organizing common methods together. Remember – this is just a working definition for now.
(7) Namespaces are a means of removing ambiguity and confusion, much like the reason for your last name, or your surname. It’s like a last name for classes. This is what Microsoft has done for all of its classes. It helps to organize their massive library of code into logical containers that help developers find what they’re looking for, in order to more easily memorize and find those methods that are actually needed from the Framework Class Library.
(8) Using statements: Typically this is placed at the very top of the code file, so that a developer doesn’t have to type the entire namespace used by the classes and the methods in the main code body.
(9) The semicolon is like the period at the end of the complete thought within the code.
(10) Indentation and code coloring are a way to help you and others read code.
If something doesn’t make sense, reread this lab. Use some of the key words and type them into a search engine and do a little more research. With a strong foundational understanding of the basics, the advanced topics will be much easier to understand.

