Tutorial by Examples: a

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[]
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...
var numbers = new[] {2,4,6,1,3,5,7,8}; var evenNumbers = numbers.TakeWhile(n => (n & 1) == 0); Console.WriteLine(string.Join(",", evenNumbers.ToArray())); //2,4,6
var numbers = new[] {2,4,6,8,1,3,5,7}; var numbersOrDefault = numbers.DefaultIfEmpty(); Console.WriteLine(numbers.SequenceEqual(numbersOrDefault)); //True var noNumbers = new int[0]; var noNumbersOrDefault = noNumbers.DefaultIfEmpty(); Console.WriteLine(noNumbersOrDefault.Count()); //1 C...
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...

as

The as keyword is an operator similar to a cast. If a cast is not possible, using as produces null rather than resulting in an InvalidCastException. expression as type is equivalent to expression is type ? (type)expression : (type)null with the caveat that as is only valid on reference conversions,...
The namespace keyword is an organization construct that helps us understand how a codebase is arranged. Namespaces in C# are virtual spaces rather than being in a physical folder. namespace StackOverflow { namespace Documentation { namespace CSharp.Keywords { ...
Cast is different from the other methods of Enumerable in that it is an extension method for IEnumerable, not for IEnumerable<T>. Thus it can be used to convert instances of the former into instances of the later. This does not compile since ArrayList does not implement IEnumerable<T>: ...
public interface IAnimal { string Name { get; set; } } public interface INoiseMaker { string MakeNoise(); } public class Cat : IAnimal, INoiseMaker { public Cat() { Name = "Cat"; } public string Name { get; set; } public string M...
public class LivingBeing { string Name { get; set; } } public interface IAnimal { bool HasHair { get; set; } } public interface INoiseMaker { string MakeNoise(); } //Note that in C#, the base class name must come before the interface names public class Cat : LivingBei...
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 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...
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...

Page 5 of 1099