This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.
Basics
Identifier An identifier is the name of a code element. It can contain letters, digits and underscores (_), and is case sensitive (FOO is different from foo). The language imposes the following restrictions on identifier names:
They cannot start with a digit; They cannot start with a symbol, except for the at sign (@), which can be used to prefix keywords to allow them as identifiers; They cannot contain more than 511 characters. Identifier names may be prefixed by @, which allows the use of reserved keywords as identifiers (for example, @class), but the @ is not considered part of the identifier's name for most purposes; @name and name refer to the same identifier. Microsoft offered naming conventions for identifiers in C#, recommending the use of PascalCase for the names of types and most type members, and camelCase for variables and for private or internal fields. These naming conventions are not enforced by the language.
Keywords Keywords are words with syntactic meaning. The language has two types of keyword — contextual and reserved. Reserved keywords such as false or byte may only be used as keywords. Contextual keywords such as where or from are treated as keywords only in certain situations. If an identifier is needed that matches a reserved keyword, it is prefixed by an @ sign to distinguish it. For example, @out is interpreted as an identifier, whereas out is a keyword. This syntax facilitates reuse of .NET code written in other languages. The following C# keywords are reserved words, of which there are 77.
Contextual keywords A contextual keyword is used to provide a specific meaning in the code, but it is not a reserved word in C#. Some contextual keywords, such as partial and where, have meanings in multiple contexts. The following 42 C# keywords are contextual.
Preprocessor directives Although C# does not have a separate preprocessor, unlike C and C++ which use the C preprocessor, these 13 directives are processed as if there were one.
Literals
Digit separators Starting in C# 7.0, the underscore symbol can be used to separate digits in number values for readability purposes. The compiler ignores these underscores.
Generally, separators may be put only between digit characters. It cannot be put at the beginning (_121) or the end of the value (121_ or 121.05_), next to the decimal in floating point values (10_.0), next to the exponent character (1.1e_1), or next to the type specifier (10_f).
Variables Variables are identifiers associated with values. They are declared by writing the variable's type and name, and are optionally initialized in the same statement. Declare
Assigning
Initialize
Multiple variables of the same type can be declared and initialized in one statement.
Local variable type inference C# 3.0 introduced type inference, allowing the type specifier of a variable declaration to be replaced by the keyword var, if its actual type can be statically determined from the initializer. This reduces repetition, especially for types with multiple generic type-parameters, and adheres more closely to the DRY principle.
Constants Constants are immutable values.
const When declaring a local variable or a field with the const keyword as a prefix the value must be given when it is declared. Thereafter the value cannot change. They can either be declared in the context as a field or a local variable. Constants are implicitly static.
This shows both uses of the keyword.
readonly The readonly keyword does a similar thing to fields. Like fields marked as const their value cannot change once initialized. The difference is that one can choose to initialize them in a constructor, or to a value that is not known until run-time. This works only on fields. readonly fields can either be members of an instance or static class members.
Code blocks Curly braces { ... } are used to signify a code block and a new scope. Class members and the body of a method are examples of what can live inside these braces in various contexts. Inside of method bodies, braces can be used to create new scopes:
Program structure A C# application consists of classes and their members. Classes and other types exist in namespaces and can be nested inside other classes.
Main method Every program must have an entry point. The entry point of a C# application is the Main method. There can only be one declaration of this method, and it is a static method in a class. It usually returns void and is passed command-line arguments as an array of strings.
The main method may return an integer value if specified.
Async Main Asynchronous Tasks can be awaited in the Main method by declaring the method's return type as System.Threading.Tasks.Task.
All combinations of System.Collections.Generic.Task, or System.Collections.Generic.Task<int>, and with, or without, the string[] args parameter are supported.
Top-level statements Similar to in scripting languages, top-level statements remove the ceremony of having to declare the Program class with a Main method. Instead, statements can be written directly in one file, which is the program entry point. Code in other files must be defined in classes.
This was introduced to make C# less verbose, and thus more accessible.Types are declared after the statements, and are available from the statements above them.
Namespaces Namespaces are a part of a type name and are used to group and/or distinguish named entities from other ones.
A namespace is defined with the namespace keyword:
A namespace can be nested inside a namespace in three ways:
and:
In C# 10 and later, namespaces can be defined using file-scoped declarations by doing the following:
using directive The using directive loads a specific namespace from a referenced assembly. It is usually placed in the top (or header) of a code file but it can be placed elsewhere, e.g. inside a class.
The directive can also be used to define another name for an existing namespace or type. This is useful when names are long.
using static directive The using static directive loads the static members of a specified type into the current scope, making them accessible directly by the name of the member.
global using directive Since C# 10, it is legal to write global using directives to indicate a using directive once and apply it to all source files.
Operators
… excerpt ends here. Continue reading the full article.

