Tutorial by Examples

The stackalloc keyword creates a region of memory on the stack and returns a pointer to the start of that memory. Stack allocated memory is automatically removed when the scope it was created in is exited. //Allocate 1024 bytes. This returns a pointer to the first byte. byte* ptr = stackalloc byte...
Adding the volatile keyword to a field indicates to the compiler that the field's value may be changed by multiple separate threads. The primary purpose of the volatile keyword is to prevent compiler optimizations that assume only single-threaded access. Using volatile ensures that the value of the ...
The fixed statement fixes memory in one location. Objects in memory are usually moving arround, this makes garbage collection possible. But when we use unsafe pointers to memory addresses, that memory must not be moved. We use the fixed statement to ensure that the garbage collector does not relo...
For classes, interfaces, delegate, array, nullable (such as int?) and pointer types, default(TheType) returns null: class MyClass {} Debug.Assert(default(MyClass) == null); Debug.Assert(default(string) == null); For structs and enums, default(TheType) returns the same as new TheType(): struct...
The readonly keyword is a field modifier. When a field declaration includes a readonly modifier, assignments to that field can only occur as part of the declaration or in a constructor in the same class. The readonly keyword is different from the const keyword. A const field can only be initialized...

as

The as keyword is an operator similar to a cast. If a cast is not possible, using as produces null rather than resulting in an InvalidCastException. expression as type is equivalent to expression is type ? (type)expression : (type)null with the caveat that as is only valid on reference conversions,...

is

Checks if an object is compatible with a given type, i.e. if an object is an instance of the BaseInterface type, or a type that derives from BaseInterface: interface BaseInterface {} class BaseClass : BaseInterface {} class DerivedClass : BaseClass {} var d = new DerivedClass(); Console.Write...
Returns the Type of an object, without the need to instantiate it. Type type = typeof(string); Console.WriteLine(type.FullName); //System.String Console.WriteLine("Hello".GetType() == type); //True Console.WriteLine("Hello".GetType() == typeof(string)); //True
const is used to represent values that will never change throughout the lifetime of the program. Its value is constant from compile-time, as opposed to the readonly keyword, whose value is constant from run-time. For example, since the speed of light will never change, we can store it in a constan...
The namespace keyword is an organization construct that helps us understand how a codebase is arranged. Namespaces in C# are virtual spaces rather than being in a physical folder. namespace StackOverflow { namespace Documentation { namespace CSharp.Keywords { ...
try, catch, finally, and throw allow you to handle exceptions in your code. var processor = new InputProcessor(); // The code within the try block will be executed. If an exception occurs during execution of // this code, execution will pass to the catch block corresponding to the exception typ...
Immediately pass control to the next iteration of the enclosing loop construct (for, foreach, do, while): for (var i = 0; i < 10; i++) { if (i < 5) { continue; } Console.WriteLine(i); } Output: 5 6 7 8 9 Live Demo on .NET Fiddle var stuff = new [] ...
The ref and out keywords cause an argument to be passed by reference, not by value. For value types, this means that the value of the variable can be changed by the callee. int x = 5; ChangeX(ref x); // The value of x could be different now For reference types, the instance in the variable can...
The checked and unchecked keywords define how operations handle mathematical overflow. "Overflow" in the context of the checked and unchecked keywords is when an integer arithmetic operation results in a value which is greater in magnitude than the target data type can represent. When ove...
goto can be used to jump to a specific line inside the code, specified by a label. goto as a: Label: void InfiniteHello() { sayHello: Console.WriteLine("Hello!"); goto sayHello; } Live Demo on .NET Fiddle Case statement: enum Permissions { Read, Write }; switch ...
The enum keyword tells the compiler that this class inherits from the abstract class Enum, without the programmer having to explicitly inherit it. Enum is a descendant of ValueType, which is intended for use with distinct set of named constants. public enum DaysOfWeek { Monday, Tuesday, ...
The base keyword is used to access members from a base class. It is commonly used to call base implementations of virtual methods, or to specify which base constructor should be called. Choosing a constructor public class Child : SomeBaseClass { public Child() : base("some string for th...
foreach is used to iterate over the elements of an array or the items within a collection which implements IEnumerable✝. var lines = new string[] { "Hello world!", "How are you doing today?", "Goodbye" }; foreach (string line in lines) { Con...
params allows a method parameter to receive a variable number of arguments, i.e. zero, one or multiple arguments are allowed for that parameter. static int AddAll(params int[] numbers) { int total = 0; foreach (int number in numbers) { total += number; } ret...
In a loop (for, foreach, do, while) the break statement aborts the execution of the innermost loop and returns to the code after it. Also it can be used with yield in which it specifies that an iterator has come to an end. for (var i = 0; i < 10; i++) { if (i == 5) { break; ...

Page 1 of 4