Tutorial by Examples: di

// 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...
Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(1, "First"); dict.Add(2, "Second"); // To safely add items (check to ensure item does not already exist - would throw) if(!dict.ContainsKey(3)) { dict.Add(3, "Third"); } Al...
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
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...
Visual Studio helps manage user and application settings. Using this approach has these benefits over using the appSettings section of the configuration file. Settings can be made strongly typed. Any type which can be serialized can be used for a settings value. Application settings can be...
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...
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...
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...
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
open System [<EntryPoint>] let main argv = printfn "Hello World" 0 Live Demo in Action at .NET Fiddle Introduction to F#
using System.Speech.Recognition; // ... SpeechRecognitionEngine recognitionEngine = new SpeechRecognitionEngine(); recognitionEngine.LoadGrammar(new DictationGrammar()); recognitionEngine.SpeechRecognized += delegate(object sender, SpeechRecognizedEventArgs e) { Console.WriteLine(&quot...
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();
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 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)...
Set function inserts a cache entry into the cache by using a CacheItem instance to supply the key and value for the cache entry. This function Overrides ObjectCache.Set(CacheItem, CacheItemPolicy) private static bool SetToCache() { string key = "Cache_Key"; string value = &quo...
This code contains an overloaded method named Hello: class Example { public static void Hello(int arg) { Console.WriteLine("int"); } public static void Hello(double arg) { Console.WriteLine("double"); } public static vo...
Snippet Console.WriteLine(nameof(CompanyNamespace.MyNamespace)); Console.WriteLine(nameof(MyClass)); Console.WriteLine(nameof(MyClass.MyNestedClass)); Console.WriteLine(nameof(MyNamespace.MyClass.MyNestedClass.MyStaticProperty)); Console Output MyNamespace MyClass MyNestedClass MyStatic...
Declaration of an interface using the interface keyword: public interface Animal { String getSound(); // Interface methods are public by default } Override Annotation @Override public String getSound() { // Code goes here... } This forces the compiler to check that we are overri...

Page 2 of 164