Tutorial by Examples: at

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...
var names = new[] {"Foo","Bar","Fizz","Buzz"}; var thirdName = names.ElementAt(2); Console.WriteLine(thirdName); //Fizz //The following throws ArgumentOutOfRangeException var minusOnethName = names.ElementAt(-1); var fifthName = names.ElementAt(4); ...
var names = new[] {"Foo","Bar","Fizz","Buzz"}; var thirdName = names.ElementAtOrDefault(2); Console.WriteLine(thirdName); //Fizz var minusOnethName = names.ElementAtOrDefault(-1); Console.WriteLine(minusOnethName); //null var fifthName = names.Eleme...
Generating a new object in each step: var elements = new[] {1,2,3,4,5}; var commaSeparatedElements = elements.Aggregate( seed: "", func: (aggregate, element) => $"{aggregate}{element},"); Console.WriteLine(commaSeparatedElements); //1,2,3,4,5, Using th...
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, catch, finally, and throw allow you to handle exceptions in your code. var processor = new InputProcessor(); // The code within the try block will be executed. If an exception occurs during execution of // this code, execution will pass to the catch block corresponding to the exception typ...
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 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...
The using static [Namespace.Type] directive allows the importing of static members of types and enumeration values. Extension methods are imported as extension methods (from just one type), not into top-level scope. 6.0 using static System.Console; using static System.ConsoleColor; using static ...
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...
The ?. operator is syntactic sugar to avoid verbose null checks. It's also known as the Safe navigation operator. Class used in the following example: public class Person { public int Age { get; set; } public string Name { get; set; } public Person Spouse { get; set; } } If a...
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...
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 : " +...
When you want to catch an exception and do something, but you can't continue execution of the current block of code because of the exception, you may want to rethrow the exception to the next exception handler in the call stack. There are good ways and bad ways to do this. private static void AskTh...
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...
public delegate int ModifyInt(int input); ModifyInt multiplyByTwo = x => x * 2; The above Lambda expression syntax is equivalent to the following verbose code: public delegate int ModifyInt(int input); ModifyInt multiplyByTwo = delegate(int x){ return x * 2; };
var now = DateTime.UtcNow; //accesses member of a class. In this case the UtcNow property.
var zipcode = myEmployee?.Address?.ZipCode; //returns null if the left operand is null. //the above is the equivalent of: var zipcode = (string)null; if (myEmployee != null && myEmployee.Address != null) zipcode = myEmployee.Address.ZipCode;
var age = GetAge(dateOfBirth); //the above calls the function GetAge passing parameter dateOfBirth.
var letters = "letters".ToCharArray(); char letter = letters[1]; Console.WriteLine("Second Letter is {0}",letter); //in the above example we take the second character from the array //by calling letters[1] //NB: Array Indexing starts at 0; i.e. the first letter would be give...

Page 2 of 442