Tutorial by Examples: all

Either search in the Visual Studio GUI: Tools > NuGet Package Manager > Manage Packages for Solution... (Visual Studio 2015) Or run this command in a Nuget Power Shell instance to install the latest stable version Install-Package Dapper Or for a specific version Install-Package Dapper...
class ToyProfiler : IProfiler { public ConcurrentDictionary<Thread, object> Contexts = new ConcurrentDictionary<Thread, object>(); public object GetContext() { object ctx; if(!Contexts.TryGetValue(Thread.CurrentThread, out ctx)) ctx = null; ...
It is possible to use await expression to apply await operator to Tasks or Task(Of TResult) in the catch and finally blocks in C#6. It was not possible to use the await expression in the catch and finally blocks in earlier versions due to compiler limitations. C#6 makes awaiting async tasks a lot e...
public class Animal { public string Name { get; set; } public Animal() : this("Dog") { } public Animal(string name) { Name = name; } } var dog = new Animal(); // dog.Name will be set to "Dog" by default. var cat = new Ani...
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...
A constructor of a base class is called before a constructor of a derived class is executed. For example, if Mammal extends Animal, then the code contained in the constructor of Animal is called first when creating an instance of a Mammal. If a derived class doesn't explicitly specify which constru...
The finally { ... } block of a try-finally or try-catch-finally will always execute, regardless of whether an exception occurred or not (except when a StackOverflowException has been thrown or call has been made to Environment.FailFast()). It can be utilized to free or clean up resources acquired i...

All

var numbers = new[] {1,2,3,4,5}; var allNumbersAreOdd = numbers.All(n => (n & 1) == 1); Console.WriteLine(allNumbersAreOdd); //False var allNumbersArePositive = numbers.All(n => n > 0); Console.WriteLine(allNumbersArePositive); //True Note that the All method functions by che...
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...
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. ...
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 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); } ...
var tasks = Enumerable.Range(1, 5).Select(n => new Task<int>(() => { Console.WriteLine("I'm task " + n); return n; })).ToArray(); foreach(var task in tasks) task.Start(); Task.WaitAll(tasks); foreach(var task in tasks) Console.WriteLine(task.Result); ...
var random = new Random(); IEnumerable<Task<int>> tasks = Enumerable.Range(1, 5).Select(n => Task.Run(() => { Console.WriteLine("I'm task " + n); return n; })); Task<int[]> task = Task.WhenAll(tasks); int[] results = await task; Console.WriteLine...
var actions = Enumerable.Range(1, 10).Select(n => new Action(() => { Console.WriteLine("I'm task " + n); if((n & 1) == 0) throw new Exception("Exception from task " + n); })).ToArray(); try { Parallel.Invoke(actions); } catch(AggregateExc...
Calling a static method: // Single argument System.Console.WriteLine("Hello World"); // Multiple arguments string name = "User"; System.Console.WriteLine("Hello, {0}!", name); Calling a static method and storing its return value: string input = System.Con...
6.0 As of C# 6.0, the await keyword can now be used within a catch and finally block. try { var client = new AsyncClient(); await client.DoSomething(); } catch (MyException ex) { await client.LogExceptionAsync(); throw; } finally { await client.CloseAsync(); } 5.06.0 P...
The left-hand operand must be nullable, while the right-hand operand may or may not be. The result will be typed accordingly. Non-nullable int? a = null; int b = 3; var output = a ?? b; var type = output.GetType(); Console.WriteLine($"Output Type :{type}"); Console.WriteLine($&q...
// Connect to a target server using your ConnectionMultiplexer instance IServer server = conn.GetServer("localhost", 6379); // Write out each key in the server foreach(var key in server.Keys()) { Console.WriteLine(key); }
using System; using System.Reflection; using System.Reflection.Emit; class DemoAssemblyBuilder { public static void Main() { // An assembly consists of one or more modules, each of which // contains zero or more types. This code creates a single-module // a...

Page 1 of 113