Tutorial by Examples: al

Either search in the Visual Studio GUI: Tools > NuGet Package Manager > Manage Packages for Solution... (Visual Studio 2015) Or run this command in a Nuget Power Shell instance to install the latest stable version Install-Package Dapper Or for a specific version Install-Package Dapper...
class ToyProfiler : IProfiler { public ConcurrentDictionary<Thread, object> Contexts = new ConcurrentDictionary<Thread, object>(); public object GetContext() { object ctx; if(!Contexts.TryGetValue(Thread.CurrentThread, out ctx)) ctx = null; ...
Sometimes the convenience of a parameter (in terms of maintenance and expressiveness), may be outweighed by its cost in performance to treat it as a parameter. For example, when page size is fixed by a configuration setting. Or a status value is matched to an enum value. Consider: var orders = conn...
All values in Redis are ultimately stored as a RedisValue type: //"myvalue" here is implicitly converted to a RedisValue type //The RedisValue type is rarely seen in practice. db.StringSet("key", "aValue");
Open Visual Studio In the toolbar, go to File → New Project Select the Console Application project type Open the file Program.cs in the Solution Explorer Add the following code to Main(): public class Program { public static void Main() { // Prints a message to the conso...
Initialize a collection type with values: var stringList = new List<string> { "foo", "bar", }; Collection initializers are syntactic sugar for Add() calls. Above code is equivalent to: var temp = new List<string>(); temp.Add("foo"); temp.A...
Equals Checks whether the supplied operands (arguments) are equal "a" == "b" // Returns false. "a" == "a" // Returns true. 1 == 0 // Returns false. 1 == 1 // Returns true. false == true // Returns false. false == false // Return...
Introduction Properties can be initialized with the = operator after the closing }. The Coordinate class below shows the available options for initializing a property: 6.0 public class Coordinate { public int X { get; set; } = 34; // get or set auto-property with initializer public ...
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> ...
public class Animal { public string Name { get; set; } public Animal() : this("Dog") { } public Animal(string name) { Name = name; } } var dog = new Animal(); // dog.Name will be set to "Dog" by default. var cat = new Ani...
The stackalloc keyword creates a region of memory on the stack and returns a pointer to the start of that memory. Stack allocated memory is automatically removed when the scope it was created in is exited. //Allocate 1024 bytes. This returns a pointer to the first byte. byte* ptr = stackalloc byte...
A constructor of a base class is called before a constructor of a derived class is executed. For example, if Mammal extends Animal, then the code contained in the constructor of Animal is called first when creating an instance of a Mammal. If a derived class doesn't explicitly specify which constru...
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 void SerializeFoo(string fileName, Foo foo) { var serializer = new XmlSerializer(typeof(Foo)); using (var stream = File.Open(fileName, FileMode.Create)) { serializer.Serialize(stream, foo); } }
public Foo DeserializeFoo(string fileName) { var serializer = new XmlSerializer(typeof(Foo)); using (var stream = File.OpenRead(fileName)) { return (Foo)serializer.Deserialize(stream); } }
The finally { ... } block of a try-finally or try-catch-finally will always execute, regardless of whether an exception occurred or not (except when a StackOverflowException has been thrown or call has been made to Environment.FailFast()). It can be utilized to free or clean up resources acquired i...

All

var numbers = new[] {1,2,3,4,5}; var allNumbersAreOdd = numbers.All(n => (n & 1) == 1); Console.WriteLine(allNumbersAreOdd); //False var allNumbersArePositive = numbers.All(n => n > 0); Console.WriteLine(allNumbersArePositive); //True Note that the All method functions by che...
var numbers = new[] {1,2,3,4,5}; var sameNumbers = new[] {1,2,3,4,5}; var sameNumbersInDifferentOrder = new[] {5,1,4,2,3}; var equalIfSameOrder = numbers.SequenceEqual(sameNumbers); Console.WriteLine(equalIfSameOrder); //True var equalIfDifferentOrder = numbers.SequenceEqual(sameNumbersInDi...

Page 1 of 269