In this tutorial, you’ll learn about the basic C# syntax, including compilation, whitespace, statements, identifiers, keywords, literals, and comments.
C# syntax is inspired by C and C++ syntax. And it has similar syntax with Java as well.
The name of the C# compiler is csc.exe.
You can run csc
manually from the command line to compile manually.
The following command compiles the Main.cs
file
To produce a library (.dll), do the following: csc /target:library Main.cs
Example:
Compile a C# program by using the command-line instead of the Microsoft Visual Studio IDE −
csc helloworld.cs
and press enter to compile your code.To compile and execute a program in C#, you just need to click the Run button or press F5 key to execute the project in Microsoft Visual Studio IDE.
Whitespace refers to the characters that do not have visible output, including:
C# compiler ignores whitespace. But you use whitespace to make the code readable.
For example, the C# compiler will treat the following code snippets the same despite their differences in the presentation:
with whitespace:
// with whitespace
bool isLight = false;
if (isLight)
{
website.EnableLightMode();
}
without whitespace:
bool isLight = false;
if (isLight){ website.EnableLightMode();}
A statement is a source code instruction that declares a type or instructs the program to do something. A simple statement is terminated by a semicolon (;
).
For example, the following code has two simple statements:
int age = 9;
Console.WriteLine("Welcome to C#");
The first statement defines an integer variable and initializes its values to 9
. The second statement prints out a message to the console window.
A block is a sequence of zero or more statements. A block starts with an opening curly brace ({
) and ends with a closing curly brace (}
).
For example, you can group the two statements above into a block like this:
{
int age = 9;
Console.WriteLine("Welcome to C#");
}
Unlike a statment, a block does not require a semicolon (;
).
In programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label.
For example:
public class XDevSpace {
static public void Main ()
{
int x;
}
}
Here the total number of identifiers present in the above example is 3 and the names of these identifiers are:
The identifier names follow these rules:
_
' (underscore).@as
is a valid identifier, but "as
" is not because it is a keyword.counter
and Counter
identifiers are different.
In c#, Keywords are differentiated into two types those are:
Reserved keywords in C# are reserved for the compiler in any part of the program.
The following table lists the available reserved keywords in the c# programming language.
abstract | bool | continue | decimal | default |
event | explicit | extern | char | checked |
class | const | break | as | base |
delegate | is | lock | long | num |
byte | case | catch | false | finally |
fixed | float | for | as | foreach |
goto | if | implicit | in | int |
interface | internal | do | double | else |
namespace | new | null | object | operator |
out | override | params | private | protected |
public | readonly | sealed | short | sizeof |
ref | return | sbyte | stackalloc | static |
string | struct | void | volatile | while |
true | try | switch | this | throw |
unchecked | unsafe | ushort | using | static |
virtual | typeof | uint | ulong | out |
In c#, Contextual keywords can be used as an identifier in a limited program context, which can be outside of the context.
The following table lists the available Contextual Keywords in the c# programming language.
add | alias | async | await | dynamic |
from | get | orderby | ascending | descending |
group | into | join | let | nameof |
global | partial | set | remove | select |
value | var | when | Where | yield |
To use a keyword as an identifier, qualify it with the @
prefix.
For instance:
class class {...} // Illegal
class @class {...} // Legal
We cannot use class as the name of a class directly, we have to add the @
before it.
The @
symbol is not part of the identifier itself. So @myVariable
is the same as myVariable.
The @
prefix is useful when using libraries written in other .NET languages that have different keywords.
Literals are primitive values in the program. For example, an integer has the following literal:
20
To form a string, you place the text inside the double quotes ("
) like this:
"Hello From X.DevSpace.com"
C# offers two different styles of source-code documentation: single-line comments, and multiline comments and XML Comments.
A single-line comment begins with a double forward slash and continues until the end of the line.
For example:
int x = 7; // Comment about assigning 7 to x
A multiline comment begins with /* and ends with */.
For example:
int x = 1; /* This is a comment that
spans two lines */
In c#, the XML Comments are defined by using ///
(triple forward slashes) and with XML formatted comment body.
For example:
///<summary>
/// This class does something.
///</summary>
public class SomeClass
{
}
The documentation comments contain XML text used to make the program documentation.
The documentation starts with three contiguous forward slashes (///
).
;
). A block starts and ends with a pair of matching curly braces ({}
).//...
), delimited comments (/*...*/
), and documenation comments (///
).