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++)
{...
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...
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...
Checks if an object is compatible with a given type, i.e. if an object is an instance of the BaseInterface type, or a type that derives from BaseInterface:
interface BaseInterface {}
class BaseClass : BaseInterface {}
class DerivedClass : BaseClass {}
var d = new DerivedClass();
Console.Write...
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
class TcpChat
{
static void Main(string[] args)
{
if(args.Length == 0)
{
Console.WriteLine("Basic TCP chat");
Console.WriteLine();
...
Assemblies are the building block of any Common Language Runtime (CLR) application.
Every type you define, together with its methods, properties and their bytecode, is compiled and packaged inside an Assembly.
using System.Reflection;
Assembly assembly = this.GetType().Assembly;
Assembl...
public class Model
{
public string Name { get; set; }
public bool? Selected { get; set; }
}
Here we have a Class with no constructor with two properties: Name and a nullable boolean property Selected. If we wanted to initialize a List<Model>, there are a few different ways to ex...
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...
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...
For null values:
Nullable<int> i = null;
Or:
int? i = null;
Or:
var i = (int?)null;
For non-null values:
Nullable<int> i = 0;
Or:
int? i = 0;
Imports System
Module Program
Public Sub Main()
Console.WriteLine("Hello World")
End Sub
End Module
Live Demo in Action at .NET Fiddle
Introduction to Visual Basic .NET
Declaring an Event
You can declare an event on any class or struct using the following syntax:
public class MyClass
{
// Declares the event for MyClass
public event EventHandler MyEvent;
// Raises the MyEvent event
public void RaiseEvent()
{
OnMyEvent();
}...
Subscription returns an IDisposable:
IDisposable subscription = emails.Subscribe(email =>
Console.WriteLine("Email from {0} to {1}", email.From, email.To));
When you are ready to unsubscribe, simply dispose the subscription:
subscription.Dispose();
The following is a bad idea because it would dispose the db variable before returning it.
public IDBContext GetDBContext()
{
using (var db = new DBContext())
{
return db;
}
}
This can also create more subtle mistakes:
public IEnumerable<Person> GetPeople(int age)...
Given an IObservable<Offer> of offers from merchants to buy or sell some type of item at a fixed price, we can match pairs of buyers and sellers as follows:
var sellers = offers.Where(offer => offer.IsSell).Select(offer => offer.Merchant);
var buyers = offers.Where(offer => offer.Is...
The following program:
class Program
{
static void Method(params Object[] objects)
{
System.Console.WriteLine(objects.Length);
}
static void Method(Object a, Object b)
{
System.Console.WriteLine("two");
}
static void Main(string[] a...
There are several places where you can use String.Format indirectly: The secret is to look for the overload with the signature string format, params object[] args, e.g.:
Console.WriteLine(String.Format("{0} - {1}", name, value));
Can be replaced with shorter version:
Console.WriteLine...