Tutorial by Examples: e

See the other (Basic) examples above. using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; namespace Demo { public static class Program { public static void Main() { using (var catalog = new ApplicationCatalog()) ...
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...
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...
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(); }...
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 ...
public class SingletonClass { public static SingletonClass Instance { get; } = new SingletonClass(); private SingletonClass() { // Put custom constructor code here } } Because the constructor is private, no new instances of SingletonClass can be made by consum...

Page 10 of 1191