Tutorial by Examples: al

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...
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...
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) { /* handle the exception */ } finally { /* Code that will be executed, regardless if an exception was thrown / caught or not */ } The try / catch / finally block can be very handy when reading from files. ...
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]; }
// 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 ->...
public class SomeClass { public void DoStuff() { } protected void DoMagic() { } } public static class SomeClassExtensions { public static void DoStuffWrapper(this SomeClass someInstance) { someInstance.DoStuff(); // ok ...
// Translates to `dict.Add(1, "First")` etc. var dict = new Dictionary<int, string>() { { 1, "First" }, { 2, "Second" }, { 3, "Third" } }; // Translates to `dict[1] = "First"` etc. // Works in C# 6.0. var dict = new Dicti...
List<int> l2 = l1.FindAll(x => x > 6); Here x => x > 6 is a lambda expression acting as a predicate that makes sure that only elements above 6 are returned.
public async Task<JobResult> GetDataFromWebAsync() { var nextJob = await _database.GetNextJobAsync(); var response = await _httpClient.GetAsync(nextJob.Uri); var pageContents = await response.Content.ReadAsStringAsync(); return await _database.SaveJobResultAsync(pageContents); } ...
// default value of boolean is false bool b; //default value of nullable boolean is null bool? z; b = true; if(b) { Console.WriteLine("Boolean has true value"); } The bool keyword is an alias of System.Boolean. It is used to declare variables to store the Boolean values, true...
public class Model { public string Name { get; set; } public bool? Selected { get; set; } } Here we have a Class with no constructor with two properties: Name and a nullable boolean property Selected. If we wanted to initialize a List<Model>, there are a few different ways to ex...
public delegate int ModifyInt(int input); ModifyInt multiplyByTwo = x => x * 2; The above Lambda expression syntax is equivalent to the following verbose code: public delegate int ModifyInt(int input); ModifyInt multiplyByTwo = delegate(int x){ return x * 2; };
var zipcode = myEmployee?.Address?.ZipCode; //returns null if the left operand is null. //the above is the equivalent of: var zipcode = (string)null; if (myEmployee != null && myEmployee.Address != null) zipcode = myEmployee.Address.ZipCode;

Page 2 of 269