Tutorial by Examples: d

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 {}. ...
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#
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...
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...
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();
string HelloWorld = "Hello World"; HelloWorld.StartsWith("Hello"); // true HelloWorld.StartsWith("Foo"); // false Finding a string within a string Using the System.String.Contains you can find out if a particular string exists within a string. The method returns ...
String.Trim() string x = " Hello World! "; string y = x.Trim(); // "Hello World!" string q = "{(Hi!*"; string r = q.Trim( '(', '*', '{' ); // "Hi!" String.TrimStart() and String.TrimEnd() string q = "{(Hi*"; string r = q.TrimStart( '{...
Unlike interfaces, which can be described as contracts for implementation, abstract classes act as contracts for extension. An abstract class cannot be instantiated, it must be extended and the resulting class (or derived class) can then be instantiated. Abstract classes are used to provide generi...
The Range and Repeat static methods on Enumerable can be used to generate simple sequences. Range Enumerable.Range() generates a sequence of integers given a starting value and a count. // Generate a collection containing the numbers 1-100 ([1, 2, 3, ..., 98, 99, 100]) var range = Enumerable.Ran...
The following is a bad idea because it would dispose the db variable before returning it. public IDBContext GetDBContext() { using (var db = new DBContext()) { return db; } } This can also create more subtle mistakes: public IEnumerable<Person> GetPeople(int age)...
The Skip method returns a collection excluding a number of items from the beginning of the source collection. The number of items excluded is the number given as an argument. If there are less items in the collection than specified in the argument then an empty collection is returned. The Take meth...
All six methods return a single value of the sequence type, and can be called with or without a predicate. Depending on the number of elements that match the predicate or, if no predicate is supplied, the number of elements in the source sequence, they behave as follows: First() Returns the fir...
var color = "Black"; var age = 4; var query = "Select * from Cats where Color = :Color and Age > :Age"; var dynamicParameters = new DynamicParameters(); dynamicParameters.Add("Color", color); dynamicParameters.Add("Age", age); using (var connectio...
Event declaration: public event EventHandler<EventArgsT> EventName; Event handler declaration: public void HandlerName(object sender, EventArgsT args) { /* Handler logic */ } Subscribing to the event: Dynamically: EventName += HandlerName; Through the Designer: Click the Events...

Page 6 of 691