Tutorial by Examples: a

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...
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...
Select allows you to apply a transformation to every element in any data structure implementing IEnumerable. Getting the first character of each string in the following list: List<String> trees = new List<String>{ "Oak", "Birch", "Beech", "Elm", ...
Many LINQ functions both operate on an IEnumerable<TSource> and also return an IEnumerable<TResult>. The type parameters TSource and TResult may or may not refer to the same type, depending on the method in question and any functions passed to it. A few examples of this are public stat...
var name = "World"; var str = $"Hello, {name}!"; //str now contains: "Hello, World!"; Behind the scenes Internally this $"Hello, {name}!" Will be compiled to something like this: string.Format("Hello, {0}!", name);
using System.Speech.Recognition; // ... SpeechRecognitionEngine recognitionEngine = new SpeechRecognitionEngine(); recognitionEngine.LoadGrammar(new DictationGrammar()); recognitionEngine.SpeechRecognized += delegate(object sender, SpeechRecognizedEventArgs e) { Console.WriteLine(&quot...
SpeechRecognitionEngine recognitionEngine = new SpeechRecognitionEngine(); GrammarBuilder builder = new GrammarBuilder(); builder.Append(new Choices("I am", "You are", "He is", "She is", "We are", "They are")); builder.Append(new Choices...
emails.Where(email => email.From == "John")
Subscription returns an IDisposable: IDisposable subscription = emails.Subscribe(email => Console.WriteLine("Email from {0} to {1}", email.From, email.To)); When you are ready to unsubscribe, simply dispose the subscription: subscription.Dispose();
emails.Subscribe(email => Console.WriteLine("Email from {0} to {1}", email.From, email.To), cancellationToken);

Page 10 of 1099