Chapter 2 demonstrated a very simple C# program. Nonetheless, that little program was complex enough that we had to skip some of the pertinent details. This chapter illuminates these details by delving more deeply into the syntax and structure of the C# language itself.
In this chapter, I discuss the type system in C#, covering built-in types such as
bool, and user-defined types (types you create) such as classes, structs, and interfaces. I also cover programming fundamentals, such as how to create and use variables and constants. I’ll then introduce enumerations, strings, identifiers, expressions, and statements.
In the second part of the chapter, I’ll explain and demonstrate the use of flow control statements, using the
if, switch, while, do...while, for, and
foreach statements. You’ll also learn about operators, including the assignment, logical, relational, and mathematical operators. I’ll finish up with a short tutorial on the C# preprocessor.
Although C# is principally concerned with the creation and manipulation of objects, it is best to start with the fundamental building blocks: the elements from which objects are created. These include the built-in types that are an intrinsic part of the C# language as well as the syntactic elements of C#.
Every variable and object in C# has a “type.” There are built-in types (e.g.,
int), and you may create your own types (e.g.,
When you create an object, you declare its type, and ...