- 3.1. Introduction
- 3.2. A Simple C# App: Displaying a Line of Text
- 3.3. Creating a Simple App in Visual Studio
- 3.4. Modifying Your Simple C# App
- 3.5. Formatting Text with Console.Write and Console.WriteLine
- 3.6. Another C# App: Adding Integers
- 3.7. Arithmetic
- 3.8. Decision Making: Equality and Relational Operators
- 3.9. Wrap-Up
3.2. A Simple C# App: Displaying a Line of Text
Let’s consider a simple app that displays a line of text. The app and its output are shown in Fig. 3.1, which illustrates several important C# language features. Each program we present in this book includes line numbers, which are not part of actual C# code. In the Before You Begin section that follows the Preface, we show how to display line numbers for your C# code. We’ll soon see that line 10 does the real work of the app—namely, displaying the phrase Welcome to C# Programming! on the screen. Let’s now do a code walkthrough of the app.
Fig. 3.1 Text-displaying app.
1
// Fig. 3.1: Welcome1.cs
2
// Text-displaying app.
3
using System;4
5
public class Welcome16
{7
// Main method begins execution of C# app
8
public static void Main( string[] args )9
{10
Console.WriteLine("Welcome to C# Programming!"
);11
}// end Main
12
}// end class Welcome1
Comments
Line 1
// Fig. 3.1: Welcome1.cs
begins with //, indicating that the remainder of the line is a comment. You’ll insert comments to document your apps and improve their readability. The C# compiler ignores comments, so they do not cause the computer to perform any action when the app is run. We begin every app with a comment indicating the figure number and the name of the file in which the app is stored.
A comment that begins with // is called a single-line comment, because it terminates at the end of the line on which it appears. A // comment also can begin in the middle of a line and continue until the end of that line (as in lines 7, 11 and 12).
Delimited comments such as
/* This is a delimited comment.
It can be split over many lines */
can be split over several lines. This type of comment begins with the delimiter /* and ends with the delimiter */. All text between the delimiters is ignored by the compiler.
Line 2
// Text-displaying app.
is a single-line comment that describes the purpose of the app.
using Directive
Line 3
using System;
is a using directive that tells the compiler where to look for a class that’s used in this app. A great strength of Visual C# is its rich set of predefined classes that you can reuse rather than “reinventing the wheel.” These classes are organized under namespaces—named collections of related classes. Collectively, .NET’s namespaces are referred to as the .NET Framework Class Library. Each using directive identifies a namespace containing predefined classes that a C# app should be able to use. The using directive in line 3 indicates that this example intends to use classes from the System namespace, which contains the predefined Console class (discussed shortly) used in line 10, and many other useful classes.
For each new .NET class we use, we indicate the namespace in which it’s located. This information is important, because it helps you locate descriptions of each class in the .NET documentation. A web-based version of this documentation can be found at
msdn.microsoft.com/en-us/library/ms229335.aspx
This can also be accessed via the Help menu. You can click the name of any .NET class or method, then press the F1 key to get more information. Finally, you can learn about the contents of a given namespace by going to
msdn.microsoft.com/namespace
So, msdn.microsoft.com/System takes you to namespace System’s documentation.
Blank Lines and Whitespace
Line 4 is simply a blank line. Blank lines and space characters make code easier to read, and together with tab characters are known as whitespace. Space characters and tabs are known specifically as whitespace characters. Whitespace is ignored by the compiler.
Class Declaration
Line 5
public class Welcome1
begins a class declaration for the class Welcome1. Every app consists of at least one class declaration that’s defined by you. These are known as user-defined classes. The class keyword introduces a class declaration and is immediately followed by the class name (Welcome1). Keywords (sometimes called reserved words) are reserved for use by C# and are always spelled with all lowercase letters. The complete list of C# keywords is shown in Fig. 3.2.
Fig. 3.2C# keywords and contextual keywords.
Class Name Convention
By convention, all class names begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName). This convention is known as upper camel casing. A class name is an identifier—a series of characters consisting of letters, digits and underscores (_) that does not begin with a digit and does not contain spaces. Some valid identifiers are Welcome1, identifier, _value and m_inputField1. The name 7button is not a valid identifier because it begins with a digit, and the name input field is not a valid identifier because it contains a space. Normally, an identifier that does not begin with a capital letter is not the name of a class. C# is case sensitive—that is, uppercase and lowercase letters are distinct, so a1 and A1 are different (but both valid) identifiers.1
public Class
In Chapters 3–9, every class we define begins with the keyword public. For now, we’ll simply require this keyword. You’ll learn more about classes in Chapter 10. When you save your public class declaration in a file, the file name is usually the class name followed by the .cs file-name extension. For our app, the file name is Welcome1.cs.
Body of a Class Declaration
A left brace (in line 6 in Fig. 3.1), {, begins the body of every class declaration. A corresponding right brace (in line 12), }, must end each class declaration. Lines 7–11 are indented. This indentation is a spacing convention. We define each spacing convention as a Good Programming Practice.
Main Method
Line 7
// Main method begins execution of C# app
is a comment indicating the purpose of lines 8–11 of the app. Line 8
public static void Main( string[] args )
is the starting point of every app. The parentheses after the identifier Main indicate that it’s an app building block called a method. Class declarations normally contain one or more methods. Method names usually follow the same capitalization conventions used for class names. For each app, one of the methods in a class must be called Main (which is typically defined as shown in line 8); otherwise, the app will not execute. Methods are able to perform tasks and return information when they complete their tasks. Keyword void (line 8) indicates that this method will not return any information after it completes its task. Later, we’ll see that many methods do return information. You’ll learn more about methods in Chapters 4 and 7. We discuss the contents of Main’s parentheses in Chapter 8. For now, simply mimic Main’s first line in your apps.
Body of a Method Declaration
The left brace in line 9 begins the body of the method declaration. A corresponding right brace must end the method’s body (line 11). Line 10 in the body of the method is indented between the braces.
Displaying a Line of Text
Line 10
Console.WriteLine( "Welcome to C# Programming!"
);
instructs the computer to perform an action—namely, to display the string of characters between the double quotation marks, which delimit the string. A string is sometimes called a character string, a message or a string literal. We refer to them simply as strings. Whitespace characters in strings are not ignored by the compiler.
Class Console provides standard input/output capabilities that enable apps to read and display text in the console window from which the app executes. The Console.WriteLine method displays a line of text in the console window. The string in the parentheses in line 10 is the argument to the method. Method Console.WriteLine performs its task by displaying its argument in the console window. When Console.WriteLine completes its task, it positions the screen cursor (the blinking symbol indicating where the next character will be displayed) at the beginning of the next line in the console window. This movement of the cursor is similar to what happens when a user presses the Enter key while typing in a text editor—the cursor moves to the beginning of the next line in the file.
Statements
The entire line 10, including Console.WriteLine, the parentheses, the argument "Welcome to C# Programming!" in the parentheses and the semicolon (;), is called a statement. Most statements end with a semicolon. When the statement in line 10 executes, it displays the message Welcome to C# Programming! in the console window. A method is typically composed of one or more statements that perform the method’s task.
Matching Left ({) and Right (}) Braces
You may find it difficult when reading or writing an app to match the left and right braces ({ and }) that delimit the body of a class declaration or a method declaration. To help, you can include a comment after each closing right brace (}) that ends a method declaration and after each closing right brace that ends a class declaration. For example, line 11
} // end Main
specifies the closing right brace of method Main, and line 12
} // end class Welcome1
specifies the closing right brace of class Welcome1. Each of these comments indicates the method or class that the right brace terminates. Visual Studio can help you locate matching braces in your code. Simply place the cursor immediately in front of the left brace or immediately after the right brace, and Visual Studio will highlight both.