Tutorial by Examples: an

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; ...
ConnectionMultiplexer conn = /* initialization */; var profiler = new ToyProfiler(); conn.RegisterProfiler(profiler); var threads = new List<Thread>(); var perThreadTimings = new ConcurrentDictionary<Thread, List<IProfiledCommand>>(); for (var i = 0; i < 16; i++) {...
IDBConnection db = /* ... */ var id = /* ... */ db.Execute(@"update dbo.Dogs set Name = 'Beowoof' where Id = @id", new { id });
A common scenario in database queries is IN (...) where the list here is generated at runtime. Most RDBMS lack a good metaphor for this - and there is no universal cross-RDBMS solution for this. Instead, dapper provides some gentle automatic command expansion. All that is requires is a supplied para...
db.StringSet("key", 11021); int i = (int)db.StringGet("key"); Or using StackExchange.Redis.Extensions: db.Add("key", 11021); int i = db.Get<int>("key");
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...
Extension methods can also be used like ordinary static class methods. This way of calling an extension method is more verbose, but is necessary in some cases. static class StringExtensions { public static string Shorten(this string text, int length) { return text.Substring(0, ...
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...
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...
Get Instance method and invoke it using System; public class Program { public static void Main() { var theString = "hello"; var method = theString .GetType() .GetMethod("Substring", ...
An interface is used to enforce the presence of a method in any class that 'implements' it. The interface is defined with the keyword interface and a class can 'implement' it by adding : InterfaceName after the class name. A class can implement multiple interfaces by separating each interface with ...
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...
Code can and should throw exceptions in exceptional circumstances. Examples of this include: Attempting to read past the end of a stream Not having necessary permissions to access a file Attempting to perform an invalid operation, such as dividing by zero A timeout occurring when downloading a...

Any

Returns true if the collection has any elements that meets the condition in the lambda expression: var numbers = new[] {1,2,3,4,5}; var isNotEmpty = numbers.Any(); Console.WriteLine(isNotEmpty); //True var anyNumberIsOne = numbers.Any(n => n == 1); Console.WriteLine(anyNumberIsOne); //Tr...
Enumerable.Select returns an output element for every input element. Whereas Enumerable.SelectMany produces a variable number of output elements for each input element. This means that the output sequence may contain more or fewer elements than were in the input sequence. Lambda expressions passe...
public class LivingBeing { string Name { get; set; } } public interface IAnimal { bool HasHair { get; set; } } public interface INoiseMaker { string MakeNoise(); } //Note that in C#, the base class name must come before the interface names public class Cat : LivingBei...
interface BaseInterface {} class BaseClass : BaseInterface {} interface DerivedInterface {} class DerivedClass : BaseClass, DerivedInterface {} var baseInterfaceType = typeof(BaseInterface); var derivedInterfaceType = typeof(DerivedInterface); var baseType = typeof(BaseClass); var derived...
try { /* code that could throw an exception */ } catch (Exception ex) { /* handle the exception */ } Note that handling all exceptions with the same code is often not the best approach. This is commonly used when any inner exception handling routines fail, as a last resort.
try { /* code to open a file */ } catch (System.IO.FileNotFoundException) { /* code to handle the file being not found */ } catch (System.IO.UnauthorizedAccessException) { /* code to handle not being allowed access to the file */ } catch (System.IO.IOException) { /* cod...
In order to be able to manage your projects' packages, you need the NuGet Package Manager. This is a Visual Studio Extension, explained in the official docs: Installing and Updating NuGet Client. Starting with Visual Studio 2012, NuGet is included in every edition, and can be used from: Tools ->...

Page 1 of 307