Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Programmers insert comments to document applications and improve their readability. The C# compiler ignores comments.
A comment that begins with // is called a single-line comment, because it terminates at the end of the line on which it appears.
Comments delimited by /* and */ can be spread over several lines.
A programming language’s syntax specifies rules for creating a proper application in that language.
A using directive helps the compiler locate a class that is used in an application.
C# provides a rich set of predefined classes that you can reuse rather than “reinventing the wheel.” These classes are grouped into namespaces—named collections of classes.
Collectively, C#’s predefined namespaces are referred to as the .NET Framework Class Library.
Programmers use blank lines and space characters to make applications easier to read. Together, blank lines, space characters and tab characters are known as whitespace. Space characters and tabs are known specifically as whitespace characters. Whitespace is ignored by the compiler.
Every application in C# consists of at least one class declaration that is defined by the programmer (also known as a user-defined class).
Keywords are reserved for use by C# and are always spelled with all lowercase letters.
Keyword class introduces a class declaration and is immediately followed by the class name.
By convention, all class names in C# begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName). The is known as upper camel casing.
A C# 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.
C# is case sensitive—that is, uppercase and lowercase letters are distinct.
The body of every class declaration is delimited by braces, { and }.
Method Main is the starting point of every C# application and is typically defined as:
public static void Main( string[] args )
Methods are able to perform tasks and can return information when they complete their tasks. Keyword void indicates that a method will perform a task but will not return any information.
Statements instruct the computer to perform actions.
A sequence of characters in double quotation marks is called a string, a character string, a message or a string literal.
The Console class allows C# applications to read and display characters in the console window.
Method Console.WriteLine displays its argument in the console window, followed by a newline character to position the screen cursor to the beginning of the next line.
Most statements end with a semicolon.
Visual C# Express provides many ways to personalize your coding experience. You can modify the editor settings to display line numbers or set code indentation.
As you type characters, in some contexts Visual C# Express highlights the first member that matches all the characters typed, then displays a tool tip containing a description of that member. This IDE feature is called IntelliSense.
To execute an application, select Debug > Start Without Debugging.
When you type a line of code and press Enter, the IDE either applies syntax-color highlighting or generates a syntax error.
Console.Write displays its argument and positions the screen cursor immediately after the last character displayed.
C# combines a backslash (\) in a string with the next character in the string to form an escape sequence. The escape sequence \n (newline) positions the cursor on the next line.
The Console.Write and Console.WriteLine methods can also display formatted data.
When a method requires multiple arguments, the arguments are separated with commas (,)—this is known as a comma-separated list.
Method Console.Write’s first argument can be a format string that may consist of fixed text and format items. Fixed text is displayed normally. Each format item is a placeholder for a value.
Format items are enclosed in curly braces and begin with a number that specifies an argument. The format item {0} is a placeholder for the first additional argument after the format string (because we start counting from 0), {1} is a placeholder for the second, and so on.
Integers are whole numbers, like −22, 7, 0 and 1024.
A variable declaration statement specifies the name and type of a variable.
A variable is a location in the computer’s memory where a value can be stored for use later in an application. Variables are typically declared with a name and a type before they’re used.
A variable’s name enables the application to access the value of the variable in memory. A variable name can be any valid identifier.
Like other statements, variable declaration statements end with a semicolon (;).
Type int is used to declare variables that will hold integer values. The range of values for an int is −2,147,483,648 to +2,147,483,647.
Types float, double, and decimal specify real numbers, and type char specifies character data. Real numbers are numbers that may contain decimal points, such as 3.4, 0.0 and −11.19. Variables of type char data represent individual characters, such as an uppercase letter (e.g., A), a digit (e.g., 7), a special character (e.g., * or %) or an escape sequence (e.g., the newline character, \n).
Types such as int, float, double, decimal, and char are often called simple types. Simple-type names are keywords; thus, they must appear in all lowercase letters.
A prompt directs the user to take a specific action.
Console method ReadLine obtains a line of text for use in an application.
Convert method ToInt32 extracts an integer from a string of characters.
The assignment operator, =, enables the application to give a value to a variable. Operator = is called a binary operator because it has two operands. An assignment statement uses an assignment operator to assign a value to a variable.
Portions of statements that have values are called expressions.
Variable names correspond to locations in the computer’s memory. Every variable has a name, a type, a size and a value.
Whenever a value is placed in a memory location, the value replaces the previous value in that location. The previous value is lost.
Most applications perform arithmetic calculations. The arithmetic operators are + (addition), - (subtraction), * (multiplication), / (division) and % (remainder).
If both operands of the division operator (/) are integers, the result is an integer
The remainder operator, %, yields the remainder after division.
Arithmetic expressions in C# must be written in straight-line form.
If an expression contains nested parentheses, the innermost set of parentheses is evaluated first.
C# applies the operators in arithmetic expressions in a precise sequence determined by the rules of operator precedence.
Associativity determines whether operators are applied from left to right or right to left.
Redundant parentheses in an expression can make an expression clearer.
A condition is an expression that can be either true or false. C#’s if statement allows an application to make a decision based on the value of a condition.
Conditions in if statements can be formed by using the equality (== and !=) and relational (>, <, >= and <=) operators.
An if statement always begins with keyword if, followed by a condition in parentheses, and expects one statement in its body.
An empty statement is a statement that does not perform a task.