Whitespace
In the C# language, spaces, tabs, and newlines are considered to be "whitespace" (so named because you see only the white of the underlying "page"). Extra whitespace is generally ignored in C# statements. You can write:
myVariable = 5;
or:
myVariable = 5;
and the compiler will treat the two statements as identical.
The key word in the preceding rule is "extra" whitespace. Some whitespace is not extra; it is required to allow the compiler to differentiate one word from another. Thus, if you were to enter:
int myVariable = 5; // no problem
or:
int myVariable=5; // no problem
both would compile, because the spaces between the identifier myVariable, the assignment operator (=), and the literal value 5 are "extra." If, however, you were to enter:
intMyVariable=5; // error
you would receive a compiler error, because the space between the keyword int and the identifier myVariable is not extra, it is required.
Another exception to the "whitespace is ignored" rule is within strings. If you write:
Console.WriteLine("Hello World");each space between "Hello" and "World" is treated as another character in the string.
Most of the time, the use of whitespace is intuitive. The key is to use whitespace to make the program more readable to the programmer; the compiler is typically indifferent.
Tip
VB programmers take note: in C# the end-of-line has no special significance; you end statements with semicolons, not newline characters. There is no line-continuation character because none is needed.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access