Tutorial by Examples: co

// Translates to `dict.Add(1, "First")` etc. var dict = new Dictionary<int, string>() { { 1, "First" }, { 2, "Second" }, { 3, "Third" } }; // Translates to `dict[1] = "First"` etc. // Works in C# 6.0. var dict = new Dicti...
string[] strings = new[] {"foo", "bar"}; object[] objects = strings; // implicit conversion from string[] to object[] This conversion is not type-safe. The following code will raise a runtime exception: string[] strings = new[] {"Foo"}; object[] objects = strings;...
public async Task<JobResult> GetDataFromWebAsync() { var nextJob = await _database.GetNextJobAsync(); var response = await _httpClient.GetAsync(nextJob.Uri); var pageContents = await response.Content.ReadAsStringAsync(); return await _database.SaveJobResultAsync(pageContents); } ...
public class Person { //Id property can be read by other classes, but only set by the Person class public int Id {get; private set;} //Name property can be retrieved or assigned public string Name {get; set;} private DateTime dob; //Date of Birth property is st...
class Program { static void Main(string[] args) { // Create 2 thread objects. We're using delegates because we need to pass // parameters to the threads. var thread1 = new Thread(new ThreadStart(() => PerformAction(1))); var thread2 = new Thread(...
var zipcode = myEmployee?.Address?.ZipCode; //returns null if the left operand is null. //the above is the equivalent of: var zipcode = (string)null; if (myEmployee != null && myEmployee.Address != null) zipcode = myEmployee.Address.ZipCode;
var letters = null; char? letter = letters?[1]; Console.WriteLine("Second Letter is {0}",letter); //in the above example rather than throwing an error because letters is null //letter is assigned the value null
Deprecated usage The ConfigurationSettings class was the original way to retrieve settings for an assembly in .NET 1.0 and 1.1. It has been superseded by the ConfigurationManager class and the WebConfigurationManager class. If you have two keys with the same name in the appSettings section of the ...
The ConfigurationManager class supports the AppSettings property, which allows you to continue reading settings from the appSettings section of a configuration file the same way as .NET 1.x supported. app.config <?xml version="1.0" encoding="utf-8"?> <configuration&gt...
Starting from a new Settings class and custom configuration section: Add an application setting named ExampleTimeout, using the time System.Timespan, and set the value to 1 minute: Save the Project Properties, which saves the Settings tab entries, as well as re-generates the custom Settings cl...
var collection = new BlockingCollection<int>(5); var random = new Random(); var producerTask = Task.Run(() => { for(int item=1; item<=10; item++) { collection.Add(item); Console.WriteLine("Produced: " + item); Thread.Sleep(random.Next(1...
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...
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...
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()) ...
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...
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...
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...
using System.Speech.Recognition; // ... SpeechRecognitionEngine recognitionEngine = new SpeechRecognitionEngine(); recognitionEngine.LoadGrammar(new DictationGrammar()); recognitionEngine.SpeechRecognized += delegate(object sender, SpeechRecognizedEventArgs e) { Console.WriteLine(&quot...

Page 2 of 248