35 Chapter 2 Learning Just Enough C# or VB.NET: Basic Syntax 36 Microsoft Visual Studio 2010: A Beginner’s Guide Key Skills & Concepts ● Learn Basics of Starting a Project ● Use the VS Editor ● Code Expressions and Statements T he .NET platform supports several different programming languages. Since all of the languages run on the same platform and share the same class libraries, language choice becomes a personal choice of preference. In other words, you can accomplish the same tasks, regardless of what programming language you use. With .NET, you have a choice of language but retain the same benefits of having all of the features of .NET available to you. Visual Studio (VS) 2010 ships with four programming languages: C#, C++, F#, and Visual Basic.NET (VB). The pronunciation of each of these languages, respectively, is See Sharp, See Plus Plus, Eff Sharp, and Vee Bee. C# and VB are the two most popular .NET programming languages and have the greatest support in VS. Therefore, this book uses both C# and VB in all examples. While you may choose one of these languages as your favorite, there is great benefit in knowing both. Most of what is written online, in magazines, and in books contains examples for either C# or VB, and sometimes, but not always, both. You might not want to miss great content because of a limited language choice. Chapter 1 danced around projects and what is available. It was important to have that overview, but I’m sure you’re eager to see some code. This chapter will be satisfying in that you’ll learn how to create a project, see what code is generated, and learn how to add code yourself. This is the first chapter of three that covers language syntax, combining each language feature with tips on how VS helps you code. You’ll start off by creating a simple project and then learn about language types and statements. Starting a Bare-Bones Project Chapter 1 described the project types that you can create. This chapter takes you a step further; actually creating a project. Because the primary focus of this chapter is on learning C# and VB, the project type will be a Console application. A Console application is very simple, allowing you to read and write text from and to the Command Prompt window. Later chapters introduce you to the project types used most, such as WPF and ASP.NET. Chapter 2: Learning Just Enough C# or VB.NET: Basic Syntax 37 To get started, open VS and select File | New | Project. You’ll see the New Project window, shown in Figure 2-1. Your first task is to select Console Application as the program type. Then set the program name to FirstProgram and specify a location of your choice for where the project will be created. Other features of the New Project window include the ability to specify the .NET Framework version, sorting options, icon size options, and a search capability. NOTE It’s often useful to choose a project location other than the default. The default is your personal “My Documents” folder, which is long to type, cumbersome to navigate to, and error prone. Choosing a shorter path helps alleviate these problems. If you’re working on a team with other developers, it’s also helpful to use a common location for projects where everyone has their files in the same location. NOTE In the example code that accompanies this book, the projects are named FirstProgramCS (containing C# examples) and FirstProgramVB (containing VB examples). You’ll see this convention, specifying the language in the project name suffix, in all of the code examples accompanying this book. Figure 2-1 The New Project window 38 Microsoft Visual Studio 2010: A Beginner’s Guide Along the very top center of the dialog shown in Figure 2-1, the .NET Framework is the set of class libraries, runtime, and languages that is the development platform supported by VS. VS allows you to target multiple versions of the .NET Framework, including versions 2.0, 3.0, 3.5, and 4.0. VS will compile your code against the version you choose. Generally, you’ll want to begin all new projects with the latest version, 4.0, because you’ll want to be able to use the newest and most productive .NET features. The primary reason for using an earlier version is if you must perform work on code that is already written for an earlier version of .NET. The sorting and searching features to the right of this selection enable you to find project types in different ways, whichever is most comfortable for you. Clicking OK will produce a Console application project in the programming language you chose, which you can see in the Solution Explorer, shown in Figure 2-2. The Solution Explorer in Figure 2-2 contains a solution, which is a container for multiple projects. Later, you’ll gain a stronger appreciation for the role of the solution when organizing projects to support a software application. Under the solution is the FirstProgram project. Within the FirstProgram project are project items, such as files and settings. Many different types of project items can go into a project, and the specific project items that go Figure 2-2 A Console application in the Solution Explorer Chapter 2: Learning Just Enough C# or VB.NET: Basic Syntax 39 into a project depend on the project type. For example, there are project items that are part of a WPF application but wouldn’t be part of a Console application. Of particular interest in the FirstProgram project is the file named Program.cs (or Module1.vb if programming in VB), which is a code file, as we’ll discuss in the next section. Examining the Code Skeleton Having run the New Project Wizard for a Console application, you’ll see a file named Program.cs (or Module.vb) that contains skeleton code in the editor. VS will create skeleton code using built-in templates for most project types that you create. Y ou’re free to add, remove, or modify this code as you see fit. Listing 2-1 contains the skeleton code, which I’ll explain next. Listing 2-1 Console application skeleton code C#: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FirstProgram { class Program { static void Main(string[] args) { } } } VB: Module Module1 Sub Main() End Sub End Module 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 Chapter 2: Learning Just Enough C# or VB.NET: Basic Syntax 41 that belong to each instance. If an object such as Customer has methods that belong to each instance, those methods are not static. However, if the Customer object type has a method that is static, then there would only be a single copy of that method that is shared among all Customer objects. For example, what if you wanted to get a discount price for all customers, regardless of who the customer is; you would declare a static method named GetCustomerDiscount. However, if you wanted information that belonged to a specific customer, such as an address, you would create an instance method named GetAddress that would not be modified as static. VB uses the term shared, which has the same meaning as static. Modules are inherently shared, and all module methods must be shared. Therefore, the VB Main method is shared. In C#, the curly braces define the begin and end of the Main method. In VB, Main begins with Sub and is scoped to End Sub. Next, notice that the C# Main method is enclosed inside of a set of braces that belong to something called a class that has been given the name Program. The VB Main method is enclosed in something called a module. You’ll learn about the enclosing class and module next. The Program Class Methods always reside inside of a type declaration. A type could be a class or struct for C# or a class, module, or struct in VB. The term type might be a little foreign to you, but it might be easier if you thought of it as something that contains things. Methods are one of the things that types contain. The following snippet, from Listing 2-1, shows the type that contains the Main method, which is a class in C# and a module (in this example) in VB: class Program { // Main Method omitted for brevity } VB: Module Module1 ' Main omitted for brevity End Module Most object types you create will be a class, as shown in the previous C# example. In VB, you would replace Module with Class. Although VS uses Module as the default object type for a new project, it’s a holdover from earlier versions of VB. In practice, you shouldn’t use the VB Module but should prefer Class. The Program class contains the Main method. You could add other methods to the Program class or Module1 module, 42 Microsoft Visual Studio 2010: A Beginner’s Guide which you’ll see many times throughout this book. The Console application defined the skeleton code class to have the name Program. In reality you can name the class anything you want. Whatever names you choose should make sense for the purpose of the class. For example, it makes sense for a class that works with customers to be named Customer and only contain methods that help you work with customers. You wouldn’t add methods for working directly with invoices, products, or anything other than customers because that would make the code in your Customer class confusing. Classes are organized with namespaces, which are discussed next. The FirstProgram Namespace A namespace helps make your class names unique and therefore unambiguous. They are like adding a middle name and surname to your first name, which makes your whole name more unique. A namespace name, however, precedes the class name, whereas your middle name and surname follow your first or given name. A namespace also helps you organize code and helps you find things in other programmers’ code. This organization helps to build libraries of code where programmers have a better chance to find what they need. The .NET platform has a huge class library that is organized into namespaces and assemblies; this will become clearer the more you program. The main .NET namespace is System, which has multiple sub-namespaces. For example, guess where you can find .NET classes for working with data? Look in System.Data. Another quick test: Where are .NET classes for working with networking protocols like TCP/IP, FTP, or HTTP? Try System.Net. Another benefit of namespaces is to differentiate between classes that have the same name in different libraries. For example, what if you bought a third-party library that has a Customer class? Think about what you would do to tell the difference between Customer classes. The solution is namespaces, because if each Customer has its own namespace, you can write code that specifies each Customer by its namespace. Always using namespaces is widely considered to be a best practice. The Program class in Listing 2-1 belongs to the FirstProgram namespace, repeated here for your convenience (in C#): namespace FirstProgram { // Program class omitted for brevity } You can put many classes inside of a namespace, where inside means within the beginning and ending braces for a namespace. . 35 Chapter 2 Learning Just Enough C# or VB.NET: Basic Syntax 36 Microsoft Visual Studio 2010: A Beginner’s Guide Key Skills & Concepts ●. of having all of the features of .NET available to you. Visual Studio (VS) 2010 ships with four programming languages: C#, C++, F#, and Visual Basic.NET (VB). The pronunciation of each of these. the code examples accompanying this book. Figure 2-1 The New Project window 38 Microsoft Visual Studio 2010: A Beginner’s Guide Along the very top center of the dialog shown in Figure 2-1, the