Tutorial by Examples: ad

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++) {...
LINQPad is great for testing database queries and includes NuGet integration. To use Dapper in LINQPad press F4 to open the Query Properties and then select Add NuGet. Search for dapper dot net and select Add To Query. You will also want to click Add namespaces and highlight Dapper to include the Ex...
string fullOrRelativePath = "testfile.txt"; string fileData; using (var reader = new StreamReader(fullOrRelativePath)) { fileData = reader.ReadToEnd(); } Note that this StreamReader constructor overload does some auto encoding detection, which may or may not conform to the ...
C# allows user-defined types to overload operators by defining static member functions using the operator keyword. The following example illustrates an implementation of the + operator. If we have a Complex class which represents a complex number: public struct Complex { public double Real ...
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 ...
The readonly keyword is a field modifier. When a field declaration includes a readonly modifier, assignments to that field can only occur as part of the declaration or in a constructor in the same class. The readonly keyword is different from the const keyword. A const field can only be initialized...
Overloading just equality operators is not enough. Under different circumstances, all of the following can be called: object.Equals and object.GetHashCode IEquatable<T>.Equals (optional, allows avoiding boxing) operator == and operator != (optional, allows using operators) When overrid...
You can enumerate through a Dictionary in one of 3 ways: Using KeyValue pairs Dictionary<int, string> dict = new Dictionary<int, string>(); foreach(KeyValuePair<int, string> kvp in dict) { Console.WriteLine("Key : " + kvp.Key.ToString() + ", Value : " +...
// Translates to `dict.Add(1, "First")` etc. var dict = new Dictionary<int, string>() { { 1, "First" }, { 2, "Second" }, { 3, "Third" } }; // Translates to `dict[1] = "First"` etc. // Works in C# 6.0. var dict = new Dicti...
Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(1, "First"); dict.Add(2, "Second"); // To safely add items (check to ensure item does not already exist - would throw) if(!dict.ContainsKey(3)) { dict.Add(3, "Third"); } Al...
using System; using System.IO; using System.Linq; using System.Net.Http; using System.Threading.Tasks; class HttpGet { private static async Task DownloadAsync(string fromUrl, string toFile) { using (var fileStream = File.OpenWrite(toFile)) { using (va...
class Program { static void Main(string[] args) { // Create 2 thread objects. We're using delegates because we need to pass // parameters to the threads. var thread1 = new Thread(new ThreadStart(() => PerformAction(1))); var thread2 = new Thread(...
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...
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...

Page 1 of 114