Tutorial by Examples: d

Expression-bodied function members allow the use of lambda expressions as member bodies. For simple members, it can result in cleaner and more readable code. Expression-bodied functions can be used for properties, indexers, methods, and operators. Properties public decimal TotalPrice => Base...
Index initializers make it possible to create and initialize objects with indexes at the same time. This makes initializing Dictionaries very easy: var dict = new Dictionary<string, int>() { ["foo"] = 34, ["bar"] = 42 }; Any object that has an indexed gette...
It is possible to use await expression to apply await operator to Tasks or Task(Of TResult) in the catch and finally blocks in C#6. It was not possible to use the await expression in the catch and finally blocks in earlier versions due to compiler limitations. C#6 makes awaiting async tasks a lot e...
Starting with C# 6, collections with indexers can be initialized by specifying the index to assign in square brackets, followed by an equals sign, followed by the value to assign. Dictionary Initialization An example of this syntax using a Dictionary: var dict = new Dictionary<string, int> ...
When a type is defined without a constructor: public class Animal { } then the compiler generates a default constructor equivalent to the following: public class Animal { public Animal() {} } The definition of any constructor for the type will suppress the default constructor genera...
The fixed statement fixes memory in one location. Objects in memory are usually moving arround, this makes garbage collection possible. But when we use unsafe pointers to memory addresses, that memory must not be moved. We use the fixed statement to ensure that the garbage collector does not relo...
Declaration: void MyGenericMethod<T1, T2, T3>(T1 a, T2 b, T3 c) { // Do something with the type parameters. } Invocation: There is no need to supply type arguements to a genric method, because the compiler can implicitly infer the type. int x =10; int y =20; string z = "tes...
Get Instance method and invoke it using System; public class Program { public static void Main() { var theString = "hello"; var method = theString .GetType() .GetMethod("Substring", ...
When an object graph is finalized, the order is the reverse of the construction. E.g. the super-type is finalized before the base-type as the following code demonstrates: class TheBaseClass { ~TheBaseClass() { Console.WriteLine("Base class finalized!"); } } ...
public class Animal { public string Name { get; set; } } public interface INoiseMaker { string MakeNoise(); } //Note that in C#, the base class name must come before the interface names public class Cat : Animal, INoiseMaker { public Cat() { Name = "Ca...
public Foo DeserializeFoo(string fileName) { var serializer = new XmlSerializer(typeof(Foo)); using (var stream = File.OpenRead(fileName)) { return (Foo)serializer.Deserialize(stream); } }
public class Dog { private const string _birthStringFormat = "yyyy-MM-dd"; [XmlIgnore] public DateTime Birth {get; set;} [XmlElement(ElementName="Birth")] public string BirthString { get { return Birth.ToString(_birthStringFormat); } ...
string requestUri = "http://www.example.com"; string responseData; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(parameters.Uri); WebResponse response = request.GetResponse(); using (StreamReader responseReader = new StreamReader(response.GetResponseStream())) { ...
string requestUri = "http://www.example.com"; string responseData; using (var client = new WebClient()) { responseData = client.DownloadString(requestUri); }
HttpClient is available through NuGet: Microsoft HTTP Client Libraries. string requestUri = "http://www.example.com"; string responseData; using (var client = new HttpClient()) { using(var response = client.GetAsync(requestUri).Result) { response.EnsureSuccessStatus...
string requestUri = "http://www.example.com"; string requestBodyString = "Request body string."; string contentType = "text/plain"; string requestMethod = "POST"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri) { Method = reque...
string requestUri = "http://www.example.com"; string requestBodyString = "Request body string."; string contentType = "text/plain"; string requestMethod = "POST"; byte[] responseBody; byte[] requestBodyBytes = Encoding.UTF8.GetBytes(requestBodyS...
HttpClient is available through NuGet: Microsoft HTTP Client Libraries. string requestUri = "http://www.example.com"; string requestBodyString = "Request body string."; string contentType = "text/plain"; string requestMethod = "POST"; var request = new ...
var persons = new[] { new {Id = 1, Name = "Foo"}, new {Id = 2, Name = "Bar"}, new {Id = 3, Name = "Fizz"}, new {Id = 4, Name = "Buzz"} }; var personsSortedByName = persons.OrderBy(p => p.Name); Console.WriteLine(string.Join(&quo...
var persons = new[] { new {Id = 1, Name = "Foo"}, new {Id = 2, Name = "Bar"}, new {Id = 3, Name = "Fizz"}, new {Id = 4, Name = "Buzz"} }; var personsSortedByNameDescending = persons.OrderByDescending(p => p.Name); Console.WriteL...

Page 2 of 691