Tutorial by Examples

using System.Text; //allows you to access classes within this namespace such as StringBuilder //without prefixing them with the namespace. i.e: //... var sb = new StringBuilder(); //instead of var sb = new System.Text.StringBuilder();
using st = System.Text; //allows you to access classes within this namespace such as StringBuilder //prefixing them with only the defined alias and not the full namespace. i.e: //... var sb = new st.StringBuilder(); //instead of var sb = new System.Text.StringBuilder();
6.0 Allows you to import a specific type and use the type's static members without qualifying them with the type name. This shows an example using static methods: using static System.Console; // ... string GetName() { WriteLine("Enter your name."); return ReadLine(); } ...
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...
The enum keyword tells the compiler that this class inherits from the abstract class Enum, without the programmer having to explicitly inherit it. Enum is a descendant of ValueType, which is intended for use with distinct set of named constants. public enum DaysOfWeek { Monday, Tuesday, ...
Structs inherit from System.ValueType, are value types, and live on the stack. When value types are passed as a parameter, they are passed by value. Struct MyStruct { public int x; public int y; } Passed by value means that the value of the parameter is copied for the method, and any...

Page 10 of 1336