Microsoft 9GD00001 Computer Accessories User Manual


 
40 Microsoft Visual Studio 2010: A Beginner’s Guide
The skeleton code in Listing 2-1 is what VS created when the new Console application
was created. It is there to give you a head start on writing your program. What you now
have is a whole computer program. This program doesn’t do much of anything at this
point, but it will actually run and then end itself. Looking at the whole program, you can
see that there are sets of nested curly braces in the C# code. The VB code has Module and
Sub with corresponding End identifiers to indicate the boundaries of a block. The braces
in C# code always come in pairs and define a block. The following explanation works
from the inside out to help you understand what this code means.
The Main Method
The innermost block of the C# code is the static void Main(string[] args) definition,
which is called a method. The method in VB is called Sub Main and is identical in
purpose. You’ll learn later that methods are one way you can group code into logical
chunks of functionality. You can think of methods as actions where you, as the method
author, tell the computer what to do. The name of this particular method is Main, which is
referred to as the entry point of the program, the place where a Console application first
starts running. Another way of thinking about Main is that this is the place your computer
first transfers control to your program. Therefore, you would want to put code inside of
Main to make your program do what you want it to.
In C#, Main must be capitalized. It’s also important to remember that C# is case-
sensitive, meaning that Main (capitalized) is not the same as main (lowercase). Although
VS capitalizes your code for you if you forget to, VB is not case-sensitive. Capitalization
is a common gotcha, especially for VB programmers learning C#.
In C#, methods can return values, such as numbers, text, or other types of values, and
the type of thing they can return is specified by you right before the method name. In VB,
a Sub (a shortened keyword derived from the term subroutine) does not return a value,
but a Function does, and you’ll see examples soon. Since Main, in the C# example, does
not return a value, the return type is replaced with the keyword void. Methods can specify
parameters for holding arguments that callers pass to the method. In the case of Main, the
parameter is an array of strings, with a variable name of args. The args parameter will
hold all of the parameters passed to this program from the command line.
One more part of the C# Main method is the static keyword, which is a modifier that
says there will only ever be a single instance of this method for the life of the program. T
o
understand instances, consider that methods are members of object types where an object
can be anything in the domain of the application you’re writing, such as a Customer,
Account, or Vehicle. Think about a company that has multiple customers. Each customer
is a separate instance, which also means that each Customer instance contains methods