Tutorial by Examples: ar

public class IHtmlStringTypeHandler : SqlMapper.TypeHandler<IHtmlString> { public override void SetValue( IDbDataParameter parameter, IHtmlString value) { parameter.DbType = DbType.String; parameter.Value = value?.ToHtmlString(); } pu...
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...
Declaration: class MyGenericClass<T1, T2, T3, ...> { // Do something with the type parameters. } Initialisation: var x = new MyGenericClass<int, char, bool>(); Usage (as the type of a parameter): void AnotherMethod(MyGenericClass<float, byte, char> arg) { ... } ...
Declaration: void MyGenericMethod<T1, T2, T3>(T1 a, T2 b, T3 c) { // Do something with the type parameters. } Invocation: There is no need to supply type arguements to a genric method, because the compiler can implicitly infer the type. int x =10; int y =20; string z = "tes...
Declaration: interface IMyGenericInterface<T1, T2, T3, ...> { ... } Usage (in inheritance): class ClassA<T1, T2, T3> : IMyGenericInterface<T1, T2, T3> { ... } class ClassB<T1, T2> : IMyGenericInterface<T1, T2, int> { ... } class ClassC<T1> : IMyGenericI...
<Store> <Articles> <Product/> <Product/> </Articles> </Store> public class Store { [XmlArray("Articles")] public List<Product> Products {get; set; } }
Returns a new dictionary from the source IEnumerable using the provided keySelector function to determine keys. Will throw an ArgumentException if keySelector is not injective(returns a unique value for each member of the source collection.) There are overloads which allow one to specify the value t...
var numbers = new[] {1,2,3,4,5,6,7,8,9,10}; var someNumbers = numbers.Where(n => n < 6); Console.WriteLine(someNumbers.GetType().Name); //WhereArrayIterator`1 var someNumbersArray = someNumbers.ToArray(); Console.WriteLine(someNumbersArray.GetType().Name); //Int32[]
string sqrt = "\u221A"; // √ string emoji = "\U0001F601"; // 😁 string text = "\u0022Hello World\u0022"; // "Hello World" string variableWidth = "\x22Hello World\x22"; // "Hello World"
Apostrophes char apostrophe = '\''; Backslash char oneBackslash = '\\';
// single character s char c = 's'; // character s: casted from integer value char c = (char)115; // unicode character: single character s char c = '\u0073'; // unicode character: smiley face char c = '\u263a';
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...
List<int> l2 = l1.FindAll(x => x > 6); Here x => x > 6 is a lambda expression acting as a predicate that makes sure that only elements above 6 are returned.
string[] strings = new[] {"foo", "bar"}; object[] objects = strings; // implicit conversion from string[] to object[] This conversion is not type-safe. The following code will raise a runtime exception: string[] strings = new[] {"Foo"}; object[] objects = strings;...
public class Person { //Id property can be read by other classes, but only set by the Person class public int Id {get; private set;} //Name property can be retrieved or assigned public string Name {get; set;} private DateTime dob; //Date of Birth property is st...
var tasks = Enumerable.Range(1, 5).Select(n => new Task<int>(() => { Console.WriteLine("I'm task " + n); return n; })).ToArray(); foreach(var task in tasks) task.Start(); Task.WaitAll(tasks); foreach(var task in tasks) Console.WriteLine(task.Result); ...
var actions = Enumerable.Range(1, 10).Select(n => new Action(() => { Console.WriteLine("I'm task " + n); if((n & 1) == 0) throw new Exception("Exception from task " + n); })).ToArray(); try { Parallel.Invoke(actions); } catch(AggregateExc...
var dateString = "2015-11-24"; var date = DateTime.ParseExact(dateString, "yyyy-MM-dd", null); Console.WriteLine(date); 11/24/2015 12:00:00 AM Note that passing CultureInfo.CurrentCulture as the third parameter is identical to passing null. Or, you can pass a specific...

Page 1 of 218