Chapter 3
Variables and Expressions

Wrox.com Code Downloads for this Chapter

You can find the wrox.com code downloads for this chapter at www.wrox.com/go/beginningvisualc#2015programming on the Download Code tab. The code is in the Chapter 3 download and individually named according to the names throughout the chapter.

To use C# effectively, it's important to understand what you're actually doing when you create a computer program. Perhaps the most basic description of a computer program is that it is a series of operations that manipulate data. This is true even of the most complicated examples, including vast, multi-featured Windows applications (such as the Microsoft Office Suite). Although this is often completely hidden from users of applications, it is always going on behind the scenes.

To illustrate this further, consider the display unit of your computer. What you see onscreen is often so familiar that it is difficult to imagine it as anything other than a “moving picture.” In fact, what you see is only a representation of some data, which in its raw form is merely a stream of 0s and 1s stashed away somewhere in the computer's memory. Any onscreen action — moving a mouse pointer, clicking on an icon, typing text into a word processor — results in the shunting around of data in memory.

Of course, simpler situations show this just as well. When using a calculator application, you are supplying data as numbers and performing operations on the numbers in much the same way as you would with paper and pencil — but a lot quicker!

If computer programs are fundamentally performing operations on data, this implies that you need a way to store that data, and some methods to manipulate it. These two functions are provided by variables and expressions, respectively, and this chapter explores what that means, both in general and specific terms.

First, though, you'll take a look at the basic syntax involved in C# programming, because you need a context in which you can learn about and use variables and expressions in the C# language.

Basic C# Syntax

The look and feel of C# code is similar to that of C++ and Java. This syntax can look quite confusing at first and it's a lot less like written English than some other languages. However, as you immerse yourself in the world of C# programming, you'll find that the style used is a sensible one, and it is possible to write very readable code without much effort.

Unlike the compilers of some other languages such as Python, C# compilers ignore additional spacing in code, whether it results from spaces, carriage returns, or tab characters (collectively known as whitespace characters). This means you have a lot of freedom in the way that you format your code, although conforming to certain rules can help make your code easier to read.

C# code is made up of a series of statements, each of which is terminated with a semicolon. Because whitespace is ignored, multiple statements can appear on one line, although for readability it is usual to add carriage returns after semicolons, to avoid multiple statements on one line. It is perfectly acceptable (and quite normal), however, to use statements that span several lines of code.

C# is a block-structured language, meaning statements are part of a block of code. These blocks, which are delimited with curly brackets ({ and }), may contain any number of statements, or none at all. Note that the curly bracket characters do not need accompanying semicolons.

For example, a simple block of C# code could take the following form:

{
   <code line 1, statement 1>;
   <code line 2, statement 2>
      <code line 3, statement 2>;
}

Here the <code line x, statement y> sections are not actual pieces of C# code; this text is used as a placeholder where C# statements would go. In this case, the second and third lines of code are part of the same statement, because there is no semicolon after the second line. Indenting the third line of code makes it easier to recognize that it is actually a continuation of the second line.

The following simple example uses indentation to clarify the C# itself. This is actually standard practice, and in fact Visual Studio automatically does this for you by default. In general, each block of code has its own level of indentation, meaning how far to the right it is. Blocks of code may be nested inside each other (that is, blocks may contain other blocks), in which case nested blocks will be indented further:

{
   <code line 1>;
   {
      <code line 2>;
      <code line 3>;
   }
   <code line 4>;
}

In addition, lines of code that are continuations of previous lines are usually indented further as well, as in the third line of code in the first code example.

Of course, this style is by no means mandatory. If you don't use it, however, you will quickly find that things can get very confusing as you move through this book!

Comments are something else you often see in C# code. A comment is not, strictly speaking, C# code at all, but it happily cohabits with it. Comments are self-explanatory: They enable you to add descriptive text to your code — in plain English (or French, German, Mongolian, and so on) — which is ignored by the compiler. When you start dealing with lengthy code sections, it's useful to add reminders about exactly what you are doing, such as “this line of code asks the user for a number” or “this code section was written by Bob.”

C# provides two ways of doing this. You can either place markers at the beginning and end of a comment or you can use a marker that means “everything on the rest of this line is a comment.” The latter method is an exception to the rule mentioned previously about C# compilers ignoring carriage returns, but it is a special case.

To indicate comments using the first method, you use /* characters at the start of the comment and */ characters at the end. These may occur on a single line, or on different lines, in which case all lines in between are part of the comment. The only thing you can't type in the body of a comment is */, because that is interpreted as the end marker. For example, the following are okay:

/* This is a comment */
/* And so…
               … is this! */

The following, however, causes problems:

/* Comments often end with "*/" characters */

Here, the end of the comment (the characters after "*/") will be interpreted as C# code, and errors will occur.

The other commenting approach involves starting a comment with //. After that, you can write whatever you like — as long as you keep to one line! The following is okay:

// This is a different sort of comment.

The following fails, however, because the second line is interpreted as C# code:

// So is this,
   but this bit isn't.

This sort of commenting is useful to document statements because both can be placed on a single line:

<A statement>;         // Explanation of statement

It was stated earlier that there are two ways of commenting C# code, but there is a third type of comment in C# — although strictly speaking this is an extension of the // syntax. You can use single-line comments that start with three / symbols instead of two, like this:

/// A special comment

Under normal circumstances, they are ignored by the compiler — just like other comments — but you can configure Visual Studio to extract the text after these comments and create a specially formatted text file when a project is compiled. You can then use it to create documentation. In order for this documentation to be created, the comments must follow the rules of XML documentation as described here https://msdn.microsoft.com/library/aa288481.aspx — a subject not covered in this book but one that is well worth learning about if you have some spare time.

A very important point about C# code is that it is case sensitive. Unlike some other languages, you must enter code using exactly the right case, because using an uppercase letter instead of a lowercase one will prevent a project from compiling. For example, consider the following line of code, taken from Chapter 2:

Console.WriteLine("The first app in Beginning C# Programming!");

This code is understood by the C# compiler, as the case of the Console.WriteLine() command is correct. However, none of the following lines of code work:

console.WriteLine("The first app in Beginning C# Programming!");
CONSOLE.WRITELINE("The first app in Beginning C# Programming!");
Console.Writeline("The first app in Beginning C# Programming!");

Here, the case used is wrong, so the C# compiler won't know what you want. Luckily, as you will soon discover, Visual Studio is very helpful when it comes to entering code, and most of the time it knows (as much as a program can know) what you are trying to do. As you type, it suggests commands that you might like to use, and it tries to correct case problems.

Basic C# Console Application Structure

Here, you'll take a closer look at the console application example from Chapter 2 (ConsoleApplication1) and break down the structure a bit. Here's the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
   class Program
   {
      static void Main(string[] args)
      {
         // Output text to the screen.
         Console.WriteLine("The first app in Beginning C# Programming!");
         Console.ReadKey();
      }
   }
}

You can immediately see that all the syntactic elements discussed in the previous section are present here — semicolons, curly braces, and comments, along with appropriate indentation.

The most important section of code at the moment is the following:

static void Main(string[] args)
{
   // Output text to the screen.
   Console.WriteLine("The first app in Beginning C# Programming!");
   Console.ReadKey();
}

This is the code that is executed when you run your console application. Well, to be more precise, the code block enclosed in curly braces is executed. The comment line doesn't do anything, as mentioned earlier; it's just there for clarity. The other two code lines output some text to the console window and wait for a response, respectively, although the exact mechanisms of this don't need to concern you for now.

Note how to achieve the code outlining functionality shown in the previous chapter, albeit for a Windows application, since it is such a useful feature. You can do this with the #region and #endregion keywords, which define the start and end of a region of code that can be expanded and collapsed. For example, you could modify the generated code for ConsoleApplication1 as follows:

#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#endregion

This enables you to collapse this code into a single line and expand it again later should you want to look at the details. The using statements contained here, and the namespace statement just underneath, are explained at the end of this chapter.

For now, don't worry about the other code in the example, because the purpose of these first few chapters is to explain basic C# syntax, so the exact method of how the application execution gets to the point where Console.WriteLine() is called is of no concern. Later, the significance of this additional code is made clear.

Variables

As mentioned earlier, variables are concerned with the storage of data. Essentially, you can think of variables in computer memory as boxes sitting on a shelf. You can put things in boxes and take them out again, or you can just look inside a box to see if anything is there. The same goes for variables; you place data in them and can take it out or look at it, as required.

Although all data in a computer is effectively the same thing (a series of 0s and 1s), variables come in different flavors, known as types. Using the box analogy again, boxes come in different shapes and sizes, so some items fit only in certain boxes. The reasoning behind this type system is that different types of data may require different methods of manipulation, and by restricting variables to individual types you can avoid mixing them up. For example, it wouldn't make much sense to treat the series of 0s and 1s that make up a digital picture as an audio file.

To use variables, you have to declare them. This means that you have to assign them a name and a type. After you have declared variables, you can use them as storage units for the type of data that you declared them to hold.

C# syntax for declaring variables merely specifies the type and variable name:

<type> <name>;

If you try to use a variable that hasn't been declared, your code won't compile, but in this case the compiler tells you exactly what the problem is, so this isn't really a disastrous error. Trying to use a variable without assigning it a value also causes an error, but, again, the compiler detects this.

Simple Types

Simple types include types such as numbers and Boolean (true or false) values that make up the fundamental building blocks for your applications. Unlike complex types, simple types cannot have children or attributes. Most of the simple types available are numeric, which at first glance seems a bit strange — surely, you only need one type to store a number?

The reason for the plethora of numeric types is because of the mechanics of storing numbers as a series of 0s and 1s in the memory of a computer. For integer values, you simply take a number of bits (individual digits that can be 0 or 1) and represent your number in binary format. A variable storing N bits enables you to represent any number between 0 and (2N − 1). Any numbers above this value are too big to fit into this variable.

For example, suppose you have a variable that can store two bits. The mapping between integers and the bits representing those integers is therefore as follows:

0 = 00
1 = 01
2 = 10
3 = 11

In order to store more numbers, you need more bits (three bits enable you to store the numbers from 0 to 7, for example).

The inevitable result of this system is that you would need an infinite number of bits to be able to store every imaginable number, which isn't going to fit in your trusty PC. Even if there were a quantity of bits you could use for every number, it surely wouldn't be efficient to use all these bits for a variable that, for example, was required to store only the numbers between 0 and 10 (because storage would be wasted). Four bits would do the job fine here, enabling you to store many more values in this range in the same space of memory.

Instead, a number of different integer types can be used to store various ranges of numbers, which take up differing amounts of memory (up to 64 bits). These types are shown in Table 3.1.

Table 3.1 Integer Types

Type Alias For Allowed Values
sbyte System.SByte Integer between −128 and 127
byte System.Byte Integer between 0 and 255
short System.Int16 Integer between −32768 and 32767
ushort System.UInt16 Integer between 0 and 65535
int System.Int32 Integer between −2147483648 and 2147483647
uint System.UInt32 Integer between 0 and 4294967295
long System.Int64 Integer between −9223372036854775808 and 9223372036854775807
ulong System.UInt64 Integer between 0 and 18446744073709551615

The u characters before some variable names are shorthand for unsigned, meaning that you can't store negative numbers in variables of those types, as shown in the Allowed Values column of the preceding table.

Of course, you also need to store floating-point values, those that aren't whole numbers. You can use three floating-point variable types: float, double, and decimal. The first two store floating points in the form 6m × 2e, where the allowed values for m and e differ for each type. decimal uses the alternative form 6m × 10e. These three types are shown in Table 3.2, along with their allowed values of m and e, and these limits in real numeric terms.

Table 3.2 Floating-point Types

Type Alias For Min M Max M Min E Max E Approx Min Value Approx Max Value
float System.Single 0 224 −149 104 1.5 × 10−45 3.4 × 1038
double System.Double 0 253 −1075 970 5.0 × 10−324 1.7 × 10308
decimal System.Decimal 0 296 −28 0 1.0 × 10−28 7.9 × 1028

In addition to numeric types, three other simple types are available (see Table 3.3).

Table 3.3 Text and Boolean Types

Type Alias For Allowed Values
char System.Char Single Unicode character, stored as an integer between 0 and 65535
bool System.Boolean Boolean value, true or false
string System.String A sequence of characters

Note that there is no upper limit on the amount of characters making up a string, because it can use varying amounts of memory.

The Boolean type bool is one of the most commonly used variable types in C#, and indeed similar types are equally prolific in code in other languages. Having a variable that can be either true or false has important ramifications when it comes to the flow of logic in an application. As a simple example, consider how many questions can be answered with true or false (or yes and no). Performing comparisons between variable values or validating input are just two of the programmatic uses of Boolean variables that you will examine very soon.

Now that you've seen these types, consider a short example that declares and uses them. In the following Try It Out you use some simple code that declares two variables, assigns them values, and then outputs these values.

Variable Naming

As mentioned in the previous section, you can't just choose any sequence of characters as a variable name. This isn't as worrying as it might sound, however, because you're still left with a very flexible naming system.

The basic variable naming rules are as follows:

  • The first character of a variable name must be either a letter, an underscore character(_), or the at symbol (@).
  • Subsequent characters may be letters, underscore characters, or numbers.

There are also certain keywords that have a specialized meaning to the C# compiler, such as the using and namespace keywords shown earlier. If you use one of these by mistake, the compiler complains, however, so don't worry about it.

For example, the following variable names are fine:

myBigVar
VAR1
_test

These are not, however:

99BottlesOfBeer
namespace
It's-All-Over

Literal Values

The previous Try It Out showed two examples of literal values: an integer (17) and a string (""myInteger" is"). The other variable types also have associated literal values, as shown in Table 3.4. Many of these involve suffixes, whereby you add a sequence of characters to the end of the literal value to specify the type desired. Some literals have multiple types, determined at compile time by the compiler based on their context (also shown in Table 3.4).

Table 3.4 Literal Values

Type(s) Category Suffix Example/Allowed Values
bool Boolean None True or false
int, uint, long, ulong Integer None 100
uint, ulong Integer u or U 100U
long, ulong Integer l or L 100L
ulong Integer ul, uL, Ul, UL, lu, lU, Lu, or LU 100UL
float Real f or F 1.5F
double Real None, d, or D 1.5
decimal Real m or M 1.5M
char Character None 'a', or escape sequence
string String None "a…a", may include escape sequences

String Literals

Earlier in the chapter, you saw a few of the escape sequences you can use in string literals. Table 3.5 lists these for reference purposes.

Table 3.5 Escape Sequences for String Literals

Escape Sequence Character Produced Unicode Value of Character
' Single quotation mark 0x0027
" Double quotation mark 0x0022
\ Backslash 0x005C
Null 0x0000
a Alert (causes a beep) 0x0007
 Backspace 0x0008
f Form feed 0x000C
New line 0x000A
Carriage return 0x000D
Horizontal tab 0x0009
v Vertical tab 0x000B

The Unicode Value of Character column of the preceding table shows the hexadecimal values of the characters as they are found in the Unicode character set. As well as the preceding, you can specify any Unicode character using a Unicode escape sequence. These consist of the standard character followed by a u and a four-digit hexadecimal value (for example, the four digits after the x in Table 3.5).

This means that the following strings are equivalent:

"Benjamin's string."
"Benjaminu0027s string."

Obviously, you have more versatility using Unicode escape sequences.

You can also specify strings verbatim. This means that all characters contained between two double quotation marks are included in the string, including end-of-line characters and characters that would otherwise need escaping. The only exception to this is the escape sequence for the double quotation mark character, which must be specified to avoid ending the string. To do this, place the @ character before the string:

@"Verbatim string literal."

This string could just as easily be specified in the normal way, but the following requires the @ character:

@"A short list:
item 1
item 2"

Verbatim strings are particularly useful in filenames, as these use plenty of backslash characters. Using normal strings, you'd have to use double backslashes all the way along the string:

"C:\Temp\MyDir\MyFile.doc"

With verbatim string literals you can make this more readable. The following verbatim string is equivalent to the preceding one:

@"C:TempMyDirMyFile.doc"

Expressions

C# contains a number of operators for this purpose. By combining operators with variables and literal values (together referred to as operands when used with operators), you can create expressions, which are the basic building blocks of computation.

The operators available range from the simple to the highly complex, some of which you might never encounter outside of mathematical applications. The simple ones include all the basic mathematical operations, such as the + operator to add two operands; the complex ones include manipulations of variable content via the binary representation of this content. There are also logical operators specifically for dealing with Boolean values, and assignment operators such as =.

This chapter focuses on the mathematical and assignment operators, leaving the logical ones for the next chapter, where you examine Boolean logic in the context of controlling program flow.

Operators can be roughly classified into three categories:

  • Unary — Act on single operands
  • Binary — Act on two operands
  • Ternary — Act on three operands

Most operators fall into the binary category, with a few unary ones, and a single ternary one called the conditional operator (the conditional operator is a logical one and is discussed in Chapter 4, “Flow Control”). Let's start by looking at the mathematical operators, which span both the unary and binary categories.

Mathematical Operators

There are five simple mathematical operators, two of which (+ and -) have both binary and unary forms. Table 3.6 lists each of these operators, along with a short example of its use and the result when it's used with simple numeric types (integer and floating point).

Table 3.6 Simple Mathematical Operators

Operator Category Example Expression Result
+ Binary var1 = var2 + var3; var1 is assigned the value that is the sum of var2 and var3.
- Binary var1 = var2 - var3; var1 is assigned the value that is the value of var3 subtracted from the value of var2.
* Binary var1 = var2 * var3; var1 is assigned the value that is the product of var2 and var3.
/ Binary var1 = var2 / var3; var1 is assigned the value that is the result of dividing var2 by var3.
% Binary var1 = var2 % var3; var1 is assigned the value that is the remainder when var2 is divided by var3.
+ Unary var1 = +var2; var1 is assigned the value of var2.
- Unary var1 = -var2; var1 is assigned the value of var2 multiplied by -1.

The examples use simple numeric types because the result can be unclear when using the other simple types. What would you expect if you added two Boolean values, for example? In this case, nothing, because the compiler complains if you try to use + (or any of the other mathematical operators) with bool variables. Adding char variables is also slightly confusing. Remember that char variables are actually stored as numbers, so adding two char variables also results in a number (of type int, to be precise). This is an example of implicit conversion, which you'll learn a lot more about shortly (along with explicit conversion), because it also applies to cases where var1, var2, and var3 are of mixed types.

The binary + operator does make sense when used with string type variables. In this case, the table entry should read as shown in Table 3.7.

Table 3.7 The String Concatenation Operator

Operator Category Example Expression Result
+ Binary var1 = var2 + var3; var1 is assigned the value that is the concatenation of the two strings stored in var2 and var3.

None of the other mathematical operators, however, work with strings.

The other two operators you should look at here are the increment and decrement operators, both of which are unary operators that can be used in two ways: either immediately before or immediately after the operand. The results obtained in simple expressions are shown in Table 3.8.

Table 3.8 Increment and Decrement Operators

Operator Category Example Expression Result
++ Unary var1 = ++var2; var1 is assigned the value of var2 + 1. var2 is incremented by 1.
-- Unary var1 = --var2; var1 is assigned the value of var2 - 1. var2 is decremented by 1.
++ Unary var1 = var2++; var1 is assigned the value of var2. var2 is incremented by 1.
-- Unary var1 = var2--; var1 is assigned the value of var2. var2 is decremented by 1.

These operators always result in a change to the value stored in their operand:

  • ++ always results in its operand being incremented by one.
  • −− always results in its operand being decremented by one.

The differences between the results stored in var1 are a consequence of the fact that the placement of the operator determines when it takes effect. Placing one of these operators before its operand means that the operand is affected before any other computation takes place. Placing it after the operand means that the operand is affected after all other computation of the expression is completed.

This merits another example! Consider this code:

int var1, var2 = 5, var3 = 6;
var1 = var2++ * --var3;

What value will be assigned to var1? Before the expression is evaluated, the -- operator preceding var3 takes effect, changing its value from 6 to 5. You can ignore the ++ operator that follows var2, as it won't take effect until after the calculation is completed, so var1 will be the product of 5 and 5, or 25.

These simple unary operators come in very handy in a surprising number of situations. They are really just shorthand for expressions such as this:

var1 = var1 + 1;

This sort of expression has many uses, particularly where looping is concerned, as shown in the next chapter. The following Try It Out provides an example demonstrating how to use the mathematical operators, and it introduces a couple of other useful concepts as well. The code prompts you to type in a string and two numbers and then demonstrates the results of performing some calculations.

Assignment Operators

So far, you've been using the simple = assignment operator, and it may come as a surprise that any other assignment operators exist at all. There are more, however, and they're quite useful! All of the assignment operators other than = work in a similar way. Like =, they all result in a value being assigned to the variable on their left side based on the operands and operators on their right side.

Table 3.9 describes the operators.

Table 3.9 Assignment Operators

Operator Category Example Expression Result
= Binary var1 = var2; var1 is assigned the value of var2.
+= Binary var1 += var2; var1 is assigned the value that is the sum of var1 and var2.
-= Binary var1 -= var2; var1 is assigned the value that is the value of var2 subtracted from the value of var1.
*= Binary var1 *= var2; var1 is assigned the value that is the product of var1 and var2.
/= Binary var1 /= var2; var1 is assigned the value that is the result of dividing var1 by var2.
%= Binary var1 %= var2; var1 is assigned the value that is the remainder when var1 is divided by var2.

As you can see, the additional operators result in var1 being included in the calculation, so code like

var1 += var2;

has exactly the same result as

var1 = var1 + var2;

Using these operators, especially when employing long variable names, can make code much easier to read.

Operator Precedence

When an expression is evaluated, each operator is processed in sequence, but this doesn't necessarily mean evaluating these operators from left to right. As a trivial example, consider the following:

var1 = var2 + var3;

Here, the + operator acts before the = operator. There are other situations where operator precedence isn't so obvious, as shown here:

var1 = var2 + var3 * var4;

In the preceding example, the * operator acts first, followed by the + operator, and finally the = operator. This is standard mathematical order, and it provides the same result as you would expect from working out the equivalent algebraic calculation on paper.

Similarly, you can gain control over operator precedence by using parentheses, as shown in this example:

var1 = (var2 + var3) * var4;

Here, the content of the parentheses is evaluated first, meaning that the + operator acts before the * operator.

Table 3.10 shows the order of precedence for the operators you've encountered so far. Operators of equal precedence (such as * and /) are evaluated from left to right.

Table 3.10 Operator Precedence

Precedence Operators
Highest ++, -- (used as prefixes); +, - (unary)
*, /, %
+, -
=, *=, /=, %=, +=, -=
Lowest ++, -- (used as postfixes)

Namespaces

Before moving on, it's worthwhile to consider one more important subject — namespaces. These are the .NET way of providing containers for application code, such that code and its contents may be uniquely identified. Namespaces are also used as a means of categorizing items in the .NET Framework. Most of these items are type definitions, such as the simple types in this chapter (System.Int32 and so on).

C# code, by default, is contained in the global namespace. This means that items contained in this code are accessible from other code in the global namespace simply by referring to them by name. You can use the namespace keyword, however, to explicitly define the namespace for a block of code enclosed in curly brackets. Names in such a namespace must be qualified if they are used from code outside of this namespace.

A qualified name is one that contains all of its hierarchical information, which basically means that if you have code in one namespace that needs to use a name defined in a different namespace, you must include a reference to this namespace. Qualified names use period characters (.) between namespace levels, as shown here:

namespace LevelOne
{
   // code in LevelOne namespace
   // name "NameOne" defined
}
// code in global namespace

This code defines one namespace, LevelOne, and a name in this namespace, NameOne (no actual code is shown here to keep the discussion general; instead, a comment appears where the definition would go). Code written inside the LevelOne namespace can simply refer to this name using NameOne — no classification is necessary. Code in the global namespace, however, must refer to this name using the classified name LevelOne.NameOne.

Note one more important point here: The using statement doesn't in itself give you access to names in another namespace. Unless the code in a namespace is in some way linked to your project, by being defined in a source file in the project or being defined in some other code linked to the project, you won't have access to the names contained. In addition, if code containing a namespace is linked to your project, then you have access to the names contained in that code, regardless of whether you use using. using simply makes it easier for you to access these names, and it can shorten otherwise lengthy code to make it more readable.

Going back to the code in ConsoleApplication1 shown at the beginning of this chapter, the following lines that apply to namespaces appear:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
   …
}

The five lines that start with the using keyword are used to declare that the System, System.Collections.Generic, System.Linq, System.Text, and System.Threading.Tasks namespaces will be used in this C# code and should be accessible from all namespaces in this file without classification. The System namespace is the root namespace for .NET Framework applications and contains all the basic functionality you need for console applications. The other four namespaces are very often used in console applications, so they are there just in case. Additionally, notice that a namespace is declared for the application code itself, ConsoleApplication1 itself.

New to C# 6 is the using static keyword. This keyword allows the inclusion of static members directly into the scope of a C# program. For example, both Try It Out code walkthroughs in this chapter have used the System.Console.WriteLine() method, which is part of the System.Console static class. Notice that in these examples it is required to include the Console class combined with the WriteLine() method. When the using static System.Console namespace is added to the list of included namespaces, accessing the WriteLine() method no longer requires the preceding static class name.

All code examples requiring the System.Console static class from this point forward include the using static System.Console keyword.

image What You Learned in This Chapter

Topic Key Concepts
Basic C# syntax C# is a case-sensitive language, and each line of code is terminated with a semicolon. Lines can be indented for ease of reading if they get too long, or to identify nested blocks. You can include non-compiled comments with // or /**/ syntax. Blocks of code can be collapsed into regions, also to ease readability.
Variables Variables are chunks of data that have a name and a type. The .NET Framework defines plenty of simple types, such as numeric and string (text) types for you to use. Variables must be declared and initialized for you to use them. You can assign literal values to variables to initialize them, and variables can be declared and initialized in a single step.
Expressions Expressions are built from operators and operands, where operators perform operations on operands. There are three types of operators — unary, binary, and ternary — that operate on 1, 2, and 3 operands, respectively. Mathematical operators perform operations on numeric values, and assignment operators place the result of an expression into a variable. Operators have a fixed precedence that determines the order in which they are processed in an expression.
Namespaces All names defined in a .NET application, including variable names, are contained in a namespace. Namespaces are hierarchical, and you often have to qualify names according to the namespace that contains them in order to access them.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset