- 3.1 Introduction
- 3.2 A Simple C# Application: Displaying a Line of Text
- 3.3 Creating a Simple Application in Visual C# Express
- 3.4 Modifying Your Simple C# Application
- 3.5 Formatting Text with Console.Write and Console.WriteLine
- 3.6 Another C# Application: Adding Integers
- 3.7 Arithmetic
- 3.8 Decision Making: Equality and Relational Operators
- 3.9 Wrap-Up
3.2 A Simple C# Application: Displaying a Line of Text
Let's consider an application that displays a line of text. (Later in this section we discuss how to compile and run an application.) The application and its output are shown in Fig. 3.1. The application illustrates several important C# language features. For your convenience, each program we present in this book includes line numbers, which are not part of actual C# code. In Section 3.3 we show how to display line numbers for your C# code in the IDE. We'll soon see that line 10 does the real work of the application—namely, displaying the phrase Welcome to C# Programming! on the screen. We now do a code walkthrough.
Line 1 begins with //, indicating that the remainder of the line is a comment. We begin every application with a comment indicating the figure number and the name of the file in which the application is stored.
Fig 3.1. Text-displaying application.
1 |
Welcome to C# Programming! |
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
|
can be spread 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. C# incorporated delimited comments and single-line comments from the C and C++ programming languages, respectively. In this book, we use only single-line comments in our programs.
Line 2 is a single-line comment that describes the purpose of the application. Line 3 is a using directive that tells the compiler where to look for a class that is used in this application. 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# application should be able to use. The using directive in line 3 indicates that this example uses 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 |
You can also place the cursor on the name of any .NET class or method, then press the F1 key to get more information.
Line 4 is simply a blank line. Blank lines, space characters and tab characters are whitespace. Space characters and tabs are known specifically as whitespace characters. Whitespace is ignored by the compiler. We use whitespace to enhance application readability.
Line 5 begins a class declaration for the class Welcome1. Every application you create consists of at least one class declaration that is 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 (also 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.2. C# keywords and contextual keywords.
C# Keywords and contextual keywords |
||||
abstract |
as |
base |
bool |
break |
byte |
case |
catch |
char |
checked |
class |
const |
continue |
decimal |
default |
delegate |
do |
double |
else |
enum |
event |
explicit |
extern |
false |
finally |
fixed |
float |
for |
foreach |
goto |
if |
implicit |
in |
int |
interface |
internal |
is |
lock |
long |
namespace |
new |
null |
object |
operator |
out |
override |
params |
private |
protected |
public |
readonly |
ref |
return |
sbyte |
sealed |
short |
sizeof |
stackalloc |
static |
string |
struct |
switch |
this |
throw |
true |
try |
typeof |
uint |
ulong |
unchecked |
unsafe |
ushort |
using |
virtual |
void |
volatile |
while |
|||
Contextual Keywords |
||||
add |
alias |
ascending |
by |
descending |
equals |
from |
get |
global |
group |
into |
join |
let |
on |
orderby |
partial |
remove |
select |
set |
value |
var |
where |
yield |
By convention, all class names begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName). This is frequently referred to as Pascal 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. Identifiers may also be preceded by the @ character. This indicates that a word should be interpreted as an identifier, even if it's a keyword (e.g., @int). This allows C# code to use code written in other .NET languages where an identifier might have the same name as a C# keyword.
The contextual keywords in Fig. 3.2 can be used as identifiers outside the contexts in which they're keywords, but for clarity this is not recommended.
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 public and non-public classes in Chapter 10, Classes and Objects: A Deeper Look. 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 application, the file name is Welcome1.cs.
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 one of the spacing conventions mentioned earlier. We define each spacing convention as a Good Programming Practice.
Line 7 is a comment indicating the purpose of lines 8–11 of the application. Line 8 is the starting point of every application. The parentheses after the identifier Main indicate that it's an application building block called a method. Class declarations normally contain one or more methods. Method names usually follow the same Pascal casing capitalization conventions used for class names. For each application, one of the methods in a class must be called Main (which is typically defined as shown in line 8); otherwise, the application 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 applications.
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 of Fig. 3.1). Line 10 in the body of the method is indented between the braces.
Line 10 displays the string of characters contained between the double quotation marks. Whitespace characters in strings are not ignored by the compiler.
Class Console provides standard input/output capabilities that enable applications to read and display text in the console window from which the application 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 displays its argument in the console window. When Console.WriteLine completes its task, it positions the screen cursor at the beginning of the next line in the console window.
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 string Welcome to C# Programming! in the console window. A method is typically composed of one or more statements that perform the method's task.
Some programmers find it difficult when reading or writing an application to match the left and right braces ({ and }) that delimit the body of a class declaration or a method declaration. For this reason, we 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 specifies the closing right brace of method Main, and line 12 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.