Tutorial by Examples: a

Notes: This example must be run in administrative mode. Only one simultaneous client is supported. For simplicity, filenames are assumed to be all ASCII (for the filename part in the Content-Disposition header) and file access errors are not handled. using System; using System.IO; using Syst...
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...
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...
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...
A task can be created by directly instantiating the Task class... var task = new Task(() => { Console.WriteLine("Task code starting..."); Thread.Sleep(2000); Console.WriteLine("...task code ending!"); }); Console.WriteLine("Starting task..."); t...
var tasks = Enumerable.Range(1, 5).Select(n => new Task<int>(() => { Console.WriteLine("I'm task " + n); return n; })).ToArray(); foreach(var task in tasks) task.Start(); Task.WaitAll(tasks); foreach(var task in tasks) Console.WriteLine(task.Result); ...
var allTasks = Enumerable.Range(1, 5).Select(n => new Task<int>(() => n)).ToArray(); var pendingTasks = allTasks.ToArray(); foreach(var task in allTasks) task.Start(); while(pendingTasks.Length > 0) { var finishedTask = pendingTasks[Task.WaitAny(pendingTasks)]; Conso...
var task1 = Task.Run(() => { Console.WriteLine("Task 1 code starting..."); throw new Exception("Oh no, exception from task 1!!"); }); var task2 = Task.Run(() => { Console.WriteLine("Task 2 code starting..."); throw new Exception("Oh ...
var task1 = Task.Run(() => { Console.WriteLine("Task 1 code starting..."); throw new Exception("Oh no, exception from task 1!!"); }); var task2 = Task.Run(() => { Console.WriteLine("Task 2 code starting..."); throw new Exception("Oh ...
var cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = cancellationTokenSource.Token; var task = new Task((state) => { int i = 1; var myCancellationToken = (CancellationToken)state; while(true) { Console.Write...
var random = new Random(); IEnumerable<Task<int>> tasks = Enumerable.Range(1, 5).Select(n => Task.Run(async() => { Console.WriteLine("I'm task " + n); await Task.Delay(random.Next(10,1000)); return n; })); Task<Task<int>> whenAnyTask = Tas...
var random = new Random(); IEnumerable<Task<int>> tasks = Enumerable.Range(1, 5).Select(n => Task.Run(() => { Console.WriteLine("I'm task " + n); return n; })); Task<int[]> task = Task.WhenAll(tasks); int[] results = await task; Console.WriteLine...
var actions = Enumerable.Range(1, 10).Select(n => new Action(() => { Console.WriteLine("I'm task " + n); if((n & 1) == 0) throw new Exception("Exception from task " + n); })).ToArray(); try { Parallel.Invoke(actions); } catch(AggregateExc...
Classes inherit from System.Object, are reference types, and live on the heap. When reference types are passed as a parameter, they are passed by reference. public Class MyClass { public int a; public int b; } Passed by reference means that a reference to the parameter is passed to t...
var dateString = "2015-11-24"; var date = DateTime.ParseExact(dateString, "yyyy-MM-dd", null); Console.WriteLine(date); 11/24/2015 12:00:00 AM Note that passing CultureInfo.CurrentCulture as the third parameter is identical to passing null. Or, you can pass a specific...
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...
When we talk about the GC and the "heap", we're really talking about what's called the managed heap. Objects on the managed heap can access resources not on the managed heap, for example, when writing to or reading from a file. Unexpected behavior can occur when, a file is opened for readi...
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...

Page 8 of 1099