Chapter 3. C# Language Fundamentals

Chapter 1 demonstrates a very simple C# program that prints the text string “Hello world!” to the console screen and provides a line-by-line analysis of that program. However, even that very simple program was complex enough that some of the details had to be skipped over. In this chapter, I’ll begin an in-depth exploration of the syntax and structure of the C# language. The syntax of a language is the order of the keywords, where you put semicolons and so forth. The semantics is what you are expressing in the code, and how your code fits together. Syntax is trivial and unimportant, but because compilers are absolute sticklers for correct syntax, novice programmers spend a lot of attention to syntax until they are comfortable. Fortunately, Visual Studio 2005 makes managing syntax much easier so that you can focus on semantics, which is far more important.

In this chapter, I’ll introduce statements and expressions, the building blocks of any program. You’ll learn about variables and constants, which let you store values for use in your program. I’ll begin an explanation of types and we’ll take a look at strings, which you saw briefly in the Hello World program.

Statements

In C#, a complete program instruction is called a statement and each statement ends with a semicolon (;) . Programs consist of sequences of statements such as:

 int myVariable; // a statement myVariable = 23; // another statement int anotherVariable = myVariable; // yet another statement ...

Get Learning C# 2005, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.