Tutorial by Examples

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...
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...
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...
Developers can be caught out by the fact that type inference doesn't work for constructors: class Tuple<T1,T2> { public Tuple(T1 value1, T2 value2) { } } var x = new Tuple(2, "two"); // This WON'T work... var y = new Tuple<int, string>(2, "t...
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)); ...
An iterator method is not executed until the return value is enumerated. It's therefore advantageous to assert preconditions outside of the iterator. public static IEnumerable<int> Count(int start, int count) { // The exception will throw when the method is called, not when the result i...
Use parentheses around the expression to the left of the => operator to indicate multiple parameters. delegate int ModifyInt(int input1, int input2); ModifyInt multiplyTwoInts = (x,y) => x * y; Similarly, an empty set of parentheses indicates that the function does not accept parameters. ...
Unlike an expression lambda, a statement lambda can contain multiple statements separated by semicolons. delegate void ModifyInt(int input); ModifyInt addOneAndTellMe = x => { int result = x + 1; Console.WriteLine(result); }; Note that the statements are enclosed in braces {}. ...
For null values: Nullable<int> i = null; Or: int? i = null; Or: var i = (int?)null; For non-null values: Nullable<int> i = 0; Or: int? i = 0;
int? i = null; if (i != null) { Console.WriteLine("i is not null"); } else { Console.WriteLine("i is null"); } Which is the same as: if (i.HasValue) { Console.WriteLine("i is not null"); } else { Console.WriteLine("i is null&quot...
Given following nullable int int? i = 10; In case default value is needed, you can assign one using null coalescing operator, GetValueOrDefault method or check if nullable int HasValue before assignment. int j = i ?? 0; int j = i.GetValueOrDefault(0); int j = i.HasValue ? i.Value : 0; The ...
See RFC 2030 for details on the SNTP protocol. using System; using System.Globalization; using System.Linq; using System.Net; using System.Net.Sockets; class SntpClient { const int SntpPort = 123; static DateTime BaseDate = new DateTime(1900, 1, 1); static void Main(string[...
Imports System Module Program Public Sub Main() Console.WriteLine("Hello World") End Sub End Module Live Demo in Action at .NET Fiddle Introduction to Visual Basic .NET
Declaring an Event You can declare an event on any class or struct using the following syntax: public class MyClass { // Declares the event for MyClass public event EventHandler MyEvent; // Raises the MyEvent event public void RaiseEvent() { OnMyEvent(); }...
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...
open System [<EntryPoint>] let main argv = printfn "Hello World" 0 Live Demo in Action at .NET Fiddle Introduction to F#
Explicit interface implementation is necessary when you implement multiple interfaces who define a common method, but different implementations are required depending on which interface is being used to call the method (note that you don't need explicit implementations if multiple interfaces share t...
// 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); }
// Connect to a target server using your ConnectionMultiplexer instance IServer server = conn.GetServer("localhost", 6379); var seq = server.Keys(); IScanningCursor scanningCursor = (IScanningCursor)seq; // Use the cursor in some way...
It is possible to use multiple nested using statements without added multiple levels of nested braces. For example: using (var input = File.OpenRead("input.txt")) { using (var output = File.OpenWrite("output.txt")) { input.CopyTo(output); } // output is ...

Page 12 of 1336