Tutorial by Examples: an

When you right-click a project (or its References folder), you can click the "Manage NuGet Packages..." option. This shows the Package Manager Dialog.
Click the menus Tools -> NuGet Package Manager -> Package Manager Console to show the console in your IDE. Official documentation here. Here you can issue, amongst others, install-package commands which installs the entered package into the currently selected "Default project": Ins...
public class SomeClass { public void DoStuff() { } protected void DoMagic() { } } public static class SomeClassExtensions { public static void DoStuffWrapper(this SomeClass someInstance) { someInstance.DoStuff(); // ok ...
Assemblies are the building block of any Common Language Runtime (CLR) application. Every type you define, together with its methods, properties and their bytecode, is compiled and packaged inside an Assembly. using System.Reflection; Assembly assembly = this.GetType().Assembly; Assembl...
string[] strings = new[] {"foo", "bar"}; object[] objects = strings; // implicit conversion from string[] to object[] This conversion is not type-safe. The following code will raise a runtime exception: string[] strings = new[] {"Foo"}; object[] objects = strings;...
When you want to catch an exception and do something, but you can't continue execution of the current block of code because of the exception, you may want to rethrow the exception to the next exception handler in the call stack. There are good ways and bad ways to do this. private static void AskTh...
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; };
using System.Text; //allows you to access classes within this namespace such as StringBuilder //without prefixing them with the namespace. i.e: //... var sb = new StringBuilder(); //instead of var sb = new System.Text.StringBuilder();
using st = System.Text; //allows you to access classes within this namespace such as StringBuilder //prefixing them with only the defined alias and not the full namespace. i.e: //... var sb = new st.StringBuilder(); //instead of var sb = new System.Text.StringBuilder();
The ConfigurationManager class supports the AppSettings property, which allows you to continue reading settings from the appSettings section of a configuration file the same way as .NET 1.x supported. app.config <?xml version="1.0" encoding="utf-8"?> <configuration&gt...
Visual Studio helps manage user and application settings. Using this approach has these benefits over using the appSettings section of the configuration file. Settings can be made strongly typed. Any type which can be serialized can be used for a settings value. Application settings can be...
A task can be created by directly instantiating the Task class... var task = new Task(() => { Console.WriteLine("Task code starting..."); Thread.Sleep(2000); Console.WriteLine("...task code ending!"); }); Console.WriteLine("Starting task..."); t...
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 allTasks = Enumerable.Range(1, 5).Select(n => new Task<int>(() => n)).ToArray(); var pendingTasks = allTasks.ToArray(); foreach(var task in allTasks) task.Start(); while(pendingTasks.Length > 0) { var finishedTask = pendingTasks[Task.WaitAny(pendingTasks)]; Conso...
var task1 = Task.Run(() => { Console.WriteLine("Task 1 code starting..."); throw new Exception("Oh no, exception from task 1!!"); }); var task2 = Task.Run(() => { Console.WriteLine("Task 2 code starting..."); throw new Exception("Oh ...
var task1 = Task.Run(() => { Console.WriteLine("Task 1 code starting..."); throw new Exception("Oh no, exception from task 1!!"); }); var task2 = Task.Run(() => { Console.WriteLine("Task 2 code starting..."); throw new Exception("Oh ...
var cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = cancellationTokenSource.Token; var task = new Task((state) => { int i = 1; var myCancellationToken = (CancellationToken)state; while(true) { Console.Write...
var random = new Random(); IEnumerable<Task<int>> tasks = Enumerable.Range(1, 5).Select(n => Task.Run(async() => { Console.WriteLine("I'm task " + n); await Task.Delay(random.Next(10,1000)); return n; })); Task<Task<int>> whenAnyTask = Tas...
Type constraints are able to force a type parameter to implement a certain interface or class. interface IType; interface IAnotherType; // T must be a subtype of IType interface IGeneric<T> where T : IType { } // T must be a subtype of IType class Generic<T> where T...

Page 2 of 307