Tutorial by Examples: basic

using StackExchange.Redis; // ... // connect to the server ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("localhost"); // select a database (by default, DB = 0) IDatabase db = connection.GetDatabase(); // run a command, in this case a GET RedisValue myVal ...
Dapper makes it easy to follow best practice by way of fully parameterized SQL. Parameters are important, so dapper makes it easy to get it right. You just express your parameters in the normal way for your RDBMS (usually @foo, ?foo or :foo) and give dapper an object that has a member called foo...
It isn't always possible to neatly package all the parameters up in a single object / call. To help with more complicated scenarios, dapper allows the param parameter to be an IDynamicParameters instance. If you do this, your custom AddParameters method is called at the appropriate time and handed t...
using System; using BasicStuff = System; using Sayer = System.Console; using static System.Console; //From C# 6 class Program { public static void Main() { System.Console.WriteLine("Ignoring usings and specifying full type name"); Console.WriteLine(&quot...
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(); ...
Using the null-coalescing operator (??) allows you to specify a default value for a nullable type if the left-hand operand is null. string testString = null; Console.WriteLine("The specified string is - " + (testString ?? "not provided")); Live Demo on .NET Fiddle This is l...
using is syntactic sugar that allows you to guarantee that a resource is cleaned up without needing an explicit try-finally block. This means your code will be much cleaner, and you won't leak non-managed resources. Standard Dispose cleanup pattern, for objects that implement the IDisposable interf...
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.
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...
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...
var collection = new BlockingCollection<int>(5); var random = new Random(); var producerTask = Task.Run(() => { for(int item=1; item<=10; item++) { collection.Add(item); Console.WriteLine("Produced: " + item); Thread.Sleep(random.Next(1...
A task can be created by directly instantiating the Task class... var task = new Task(() => { Console.WriteLine("Task code starting..."); Thread.Sleep(2000); Console.WriteLine("...task code ending!"); }); Console.WriteLine("Starting task..."); t...
using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel.Composition; namespace Demo { [Export(typeof(IUserProvider))] public sealed class UserProvider : IUserProvider { public ReadOnlyCollection<User> GetAllUsers() ...
using System; using System.ComponentModel.Composition; namespace Demo { public sealed class UserWriter { [Import(typeof(IUserProvider))] private IUserProvider userProvider; public void PrintAllUsers() { foreach (User user in this.user...
See the other (Basic) examples above. using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; namespace Demo { public static class Program { public static void Main() { using (var catalog = new ApplicationCatalog()) ...
1 - Create an empty folder, it will contain the files created in the next steps. 2 - Create a file named project.json with the following content (adjust the port number and rootDirectory as appropriate): { "dependencies": { "Microsoft.AspNet.Server.Kestrel": "1.0.0...
See RFC 2030 for details on the SNTP protocol. using System; using System.Globalization; using System.Linq; using System.Net; using System.Net.Sockets; class SntpClient { const int SntpPort = 123; static DateTime BaseDate = new DateTime(1900, 1, 1); static void Main(string[...
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
// Connect to a target server using your ConnectionMultiplexer instance IServer server = conn.GetServer("localhost", 6379); // Write out each key in the server foreach(var key in server.Keys()) { Console.WriteLine(key); }
This code contains an overloaded method named Hello: class Example { public static void Hello(int arg) { Console.WriteLine("int"); } public static void Hello(double arg) { Console.WriteLine("double"); } public static vo...

Page 1 of 43