The following C# keywords are contextual:[5] Starting in C# 7.0, the underscore symbol can be used to separate digits in number values for readability purposes.
This reduces repetition, especially for types with multiple generic type-parameters, and adheres more closely to the DRY principle.
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: A C# application consists of classes and their members.
Similar to in scripting languages, top-level statements removes the ceremony of having to declare the Program class with a Main method.
C# 8.0 introduces null-coalescing assignment, such that is equivalent to C# inherits most of the control structures of C/C++ and also adds new ones like the foreach statement.
Simple one-line statement: Multi-line with else-block (without any braces): Recommended coding conventions for an if-statement.
The exception is propagated upwards through the call stack until a matching catch block is found within one of the currently active methods.
The System.String class, or simply string, represents an immutable sequence of unicode characters (char).
The type of the event argument-object derive from the EventArgs class that is a part of the CLI base library.
Dynamic denotes a variable with an object with a type that is resolved at runtime, as opposed to compile-time, as normally is done.
For example, while both offer an implicit default constructor which takes no arguments, one cannot redefine it for structs.
A method has a return value, a name and usually some parameters initialized when it is called with some arguments.
One can explicitly make arguments be passed by reference when calling a method with parameters preceded by keywords ref or out.
These managed pointers come in handy when passing variables that one wants to be modified inside the method by reference.
Fields, or instance variables, can be declared inside the class body to store data.
Modifiers for fields: Properties bring field-like syntax and combine them with the power of methods.
Methods marked virtual provide an implementation, but they can be overridden by the inheritors by using the override keyword.
The main advantage is that one can use generic type parameters to create classes and methods that can be used without incurring the cost of runtime casts or boxing operations, as shown here:[16] When compared with C++ templates, C# generics can provide enhanced safety, but also have somewhat limited capabilities.
When instantiating (or calling) a generic, one can then substitute a concrete type for the type-parameter one gave in its declaration.
Enumerators are typically obtained by calling the GetEnumerator() method of an object implementing the IEnumerable interface.
LINQ, short for Language Integrated Queries, is a .NET Framework feature which simplifies the handling of data.
Anonymous methods, or in their present form more commonly referred to as "lambda expressions", is a feature which allows programmers to write inline closure-like functions in their code.
Lambda expressions provide a simple syntax for inline functions that are similar to closures.
Multi-statement lambdas have bodies enclosed by braces and inside of them code can be written like in standard methods.
Lambda expressions can be passed as arguments directly in method calls similar to anonymous delegates but with a more aesthetic syntax.
It works by marking a block of code as a critical section by mutual exclusion of access to a provided object.
This, however, is a convention used by Visual Studio and is not part of the language definition: C#'s documentation comments[25] are similar to Java's Javadoc, but based on XML.
Single-line documentation comments, such as those commonly found in Visual Studio generated code, are indicated on a line beginning with ///.
Consider this code that takes advantage of the task library directly: Here is the same logic written in the async-await syntax: Spec# is a dialect of C# that is developed in parallel with the standard implementation from Microsoft.