Tutorial by Examples: d

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 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 ...
Structs inherit from System.ValueType, are value types, and live on the stack. When value types are passed as a parameter, they are passed by value. Struct MyStruct { public int x; public int y; } Passed by value means that the value of the parameter is copied for the method, and any...
Classes inherit from System.Object, are reference types, and live on the heap. When reference types are passed as a parameter, they are passed by reference. public Class MyClass { public int a; public int b; } Passed by reference means that a reference to the parameter is passed to t...
An enum is a special type of class. The enum keyword tells the compiler that this class inherits from the abstract System.Enum class. Enums are used for distinct lists of items. public enum MyEnum { Monday = 1, Tuesday, Wednesday, //... } You can think of an enum as a conve...
When passing formal arguments to a generic method, relevant generic type arguments can usually be inferred implicitly. If all generic type can be inferred, then specifying them in the syntax is optional. Consider the following generic method. It has one formal parameter and one generic type paramet...
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...
When we talk about the GC and the "heap", we're really talking about what's called the managed heap. Objects on the managed heap can access resources not on the managed heap, for example, when writing to or reading from a file. Unexpected behavior can occur when, a file is opened for readi...
It is possible to specify whether or not the type argument should be a reference type or a value type by using the respective constraints class or struct. If these constraints are used, they must be defined before all other constraints (for example a parent type or new()) can be listed. // TRef mus...
By using the new() constraint, it is possible to enforce type parameters to define an empty (default) constructor. class Foo { public Foo () { } } class Bar { public Bar (string s) { ... } } class Factory<T> where T : new() { public T Create() { re...
Every method has a unique signature consisting of a accessor (public, private, ...) ,optional modifier (abstract), a name and if needed method parameters. Note, that the return type is not part of the signature. A method prototype looks like the following: AccessModifier OptionalModifier ReturnTyp...
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...
A method can declare any number of parameters (in this example, i, s and o are the parameters): static void DoSomething(int i, string s, object o) { Console.WriteLine(String.Format("i={0}, s={1}, o={2}", i, s, o)); } Parameters can be used to pass values into a method, so that th...
You can use default parameters if you want to provide the option to leave out parameters: static void SaySomething(string what = "ehh") { Console.WriteLine(what); } static void Main() { // prints "hello" SaySomething("hello"); // prints &quot...
Just like other methods, extension methods can use generics. For example: static class Extensions { public static bool HasMoreThanThreeElements<T>(this IEnumerable<T> enumerable) { return enumerable.Take(4).Count() > 3; } } and calling it would be like: ...
class Program { static void Main(string[] args) { // Run 2 Tasks. var task1 = Task.Run(() => PerformAction(1))); var task2 = Task.Run(() => PerformAction(2))); // Wait (i.e. block this thread) until both Tasks are complete. Task.WaitA...
1 - Create an empty folder, it will contain the files created in the next steps. 2 - Create a file named project.json with the following content (adjust the port number and rootDirectory as appropriate): { "dependencies": { "Microsoft.AspNet.Server.Kestrel": "1.0.0...
Typically lambdas are used for defining simple functions (generally in the context of a linq expression): var incremented = myEnumerable.Select(x => x + 1); Here the return is implicit. However, it is also possible to pass actions as lambdas: myObservable.Do(x => Console.WriteLine(x)); ...

Page 5 of 691