C# – Extension Methods

Extension Methods Allow existing type to be extended with new methods without altering the definition of the original type. An extension method is a static method of a static class, where modifier is applied to the first parameter. // Extension methods allow an existing type to be extended with new methods without altering // the […]

C# – Operator Overloading

Operator Overloading Operators can be overloaded to provide more natural syntax for custom types. The following operators can be overloaded: + (unary) – (unary) ! ~ ++ — + – * / % & | ^ << >> == != > < >= <= The following operators are also overloadable: Implicit and explicit conversions The […]

C# – Enumeration and Iterators

Enumeration and Iterators Enumeration Read-only, forward-only cursor over a sequence of values. Enumerator object implements one of the interfaces: System.Collections.IEnumerator System.Collections.Generics.IEnumerator<T> Enumerable object: Iterates using foreach statement Implements IEnumerable or IEnumerable<T> Has a method named GetEnumerator that returns an enumerator The enumeration pattern: Implements IEnumerator or IEnumerator<T> class Enumerator { public IteratorVariableType Current { get […]

C# – try Statements and Exceptions

try Statements and Exceptions Try statement specifies a code block subject to error-handling. Try block must be followed by a catch block , finally block or both. catch block Executes when an error occurs in the try block, access an Exception object that contains information about the error, however you can use to compensate the […]

C# – Lambda Expression

Lambda Expression Unnamed method written in place of a delegate instance. Compiler converts lambda expression either to delegate instance or expression tree. Lambda expressions are used most commonly with the Func and Action delegates. Lambda Expression (parameters) => expression-or-statement-block Note: If there is only one parameter than parentheses can be omitted. delegate int Transformer (int […]

C# – Events

Events Two major roles attached when using delegates are: broadcaster – contains delegate field and decides when to broadcast by invoking the delegate. subscriber – method target recipients, when to commerce and halt listening by calling += and -= on the broadcaster’s delegate. An event is a construct which exposes just the subset of delegate […]

C# – Delegates

Delegates An object that knows how to call a method. A delegate type defines the kind of method that delegate instances can call (return types and parameter types). Assigning a method to a delegate variable creates a delegate instance which can be invoked same way as method. A delegate instance acts as a delegate for […]

C# – Generics

Generics Generics can reduce casting and boxing and increase type safety. Generic Types A generic type declares type parameters—placeholder types to be filled in by the consumer of the generic type. Stack<T> Example: public class Stack { int position; T[] data = new T[100]; public void Push (T obj) => data[position++] = obj; public T […]

C# – Nested Types

Nested Types Nested type is declared within the scope of another type. It can access the enclosing type’s private members and everything else the enclosing type can access. It can be declared with the full range of access modifiers, rather than just public and internal. The default accessibility for a nested type is private rather […]

C# – Enums

Enums System.Enum Special value types that specifies a group of named numeric constants. It has a underlying values of type int The constants are automatically assigned and can be specified explicitly also. public enum BorderSide { Left, Right, Top, Bottom } static void Main() { BorderSide topSide = BorderSide.Top; bool isTop = (topSide == BorderSide.Top); […]