Tutorial by Examples: e

Using the null-coalescing operator (??) allows you to specify a default value for a nullable type if the left-hand operand is null. string testString = null; Console.WriteLine("The specified string is - " + (testString ?? "not provided")); Live Demo on .NET Fiddle This is l...
using is syntactic sugar that allows you to guarantee that a resource is cleaned up without needing an explicit try-finally block. This means your code will be much cleaner, and you won't leak non-managed resources. Standard Dispose cleanup pattern, for objects that implement the IDisposable interf...
The using static [Namespace.Type] directive allows the importing of static members of types and enumeration values. Extension methods are imported as extension methods (from just one type), not into top-level scope. 6.0 using static System.Console; using static System.ConsoleColor; using static ...
Overloading just equality operators is not enough. Under different circumstances, all of the following can be called: object.Equals and object.GetHashCode IEquatable<T>.Equals (optional, allows avoiding boxing) operator == and operator != (optional, allows using operators) When overrid...
To make a class support collection initializers, it must implement IEnumerable interface and have at least one Add method. Since C# 6, any collection implementing IEnumerable can be extended with custom Add methods using extension methods. class Program { static void Main() { va...
Extension methods are static methods which behave like instance methods. However, unlike what happens when calling an instance method on a null reference, when an extension method is called with a null reference, it does not throw a NullReferenceException. This can be quite useful in some scenarios....
string sqrt = "\u221A"; // √ string emoji = "\U0001F601"; // 😁 string text = "\u0022Hello World\u0022"; // "Hello World" string variableWidth = "\x22Hello World\x22"; // "Hello World"
Apostrophes char apostrophe = '\''; Backslash char oneBackslash = '\\';
Backslash // The filename will be c:\myfile.txt in both cases string filename = "c:\\myfile.txt"; string filename = @"c:\myfile.txt"; The second example uses a verbatim string literal, which doesn't treat the backslash as an escape character. Quotes string text = "\&...
try { /* code that could throw an exception */ } catch (Exception ex) { /* handle the exception */ } Note that handling all exceptions with the same code is often not the best approach. This is commonly used when any inner exception handling routines fail, as a last resort.
try { /* code to open a file */ } catch (System.IO.FileNotFoundException) { /* code to handle the file being not found */ } catch (System.IO.UnauthorizedAccessException) { /* code to handle not being allowed access to the file */ } catch (System.IO.IOException) { /* cod...
You are allowed to create and throw exceptions in your own code. Instantiating an exception is done the same way that any other C# object. Exception ex = new Exception(); // constructor with an overload that takes a message string Exception ex = new Exception("Error message"); Yo...
The ?. operator is syntactic sugar to avoid verbose null checks. It's also known as the Safe navigation operator. Class used in the following example: public class Person { public int Age { get; set; } public string Name { get; set; } public Person Spouse { get; set; } } If a...
Similarly to the ?. operator, the null-conditional index operator checks for null values when indexing into a collection that may be null. string item = collection?[index]; is syntactic sugar for string item = null; if(collection != null) { item = collection[index]; }
// assign string from a string literal string s = "hello"; // assign string from an array of characters char[] chars = new char[] { 'h', 'e', 'l', 'l', 'o' }; string s = new string(chars, 0, chars.Length); // assign string from a char pointer, derived from a string string s; uns...
// single character s char c = 's'; // character s: casted from integer value char c = (char)115; // unicode character: single character s char c = '\u0073'; // unicode character: smiley face char c = '\u263a';
// assigning a signed short to its minimum value short s = -32768; // assigning a signed short to its maximum value short s = 32767; // assigning a signed int to its minimum value int i = -2147483648; // assigning a signed int to its maximum value int i = 2147483647; // assigning a s...
// assigning an unsigned short to its minimum value ushort s = 0; // assigning an unsigned short to its maximum value ushort s = 65535; // assigning an unsigned int to its minimum value uint i = 0; // assigning an unsigned int to its maximum value uint i = 4294967295; // assigning an...
In order to be able to manage your projects' packages, you need the NuGet Package Manager. This is a Visual Studio Extension, explained in the official docs: Installing and Updating NuGet Client. Starting with Visual Studio 2012, NuGet is included in every edition, and can be used from: Tools ->...
When you right-click a project (or its References folder), you can click the "Manage NuGet Packages..." option. This shows the Package Manager Dialog.

Page 6 of 1191