Extremely Serious

Category: C#

Extension Methods

The method that allows to add methods to existing type without creating a new derived type, recompiling, or otherwise modifying the original type.

A method signature template with a return value and an argument

public static <T_RETURN> <METHOD_NAME>(this <T_TYPE> <VAR_NAME>, <T_ARG1_TYPE> <ARG1_NAME>)
Token Description
T_RETURN The type of the return value.
METHOD_NAME The desired name of the method.
T_TYPE The existing type to extend.
VAR_NAME The holder of an instance of the T_TYPE
T_ARG1_TYPE Type of the first argument.
ARG1_NAME The name of the first argument.

This method is actually a static method but the first argument has the keyword this and the target type to extend. Also it holds the instance of the target type.

The T_RETURN and the argument part of the template are optional. Also the argument is not limited to just one, you can have as many as required by your extension.

Class Property

Description

A member of a class that provides flexibility for exposing private fields.

Sample Property Declaration

//backing private field
private string prop;

public string ReadWriteProp {
    get {
        return prop;
    }
    set {
        prop = value;
    }
}

public string ReadOnlyProp {
    get {
        return prop;
    }
}

public string WriteOnlyProp {
    set {
        prop = value;
    }
}

Sample Auto-Implemented Property Declaration

A more concise property declaration especially if theres no additional logic required for the accessors.

public string ReadWriteProp {
    get; set;
} 

public string ReadOnlyProp {
    get; private set;
} 

public string WriteOnlyProp {
    private get; set;
}

The Delegate Type

Definition

Delegate is a type that references methods with a particular parameter list and return type.

Printer Custom Delegate

public delegate void Printer<T>(T data);

A Method for Printer Delegate

static void ConsoleWrite<T>(T data) {
    Console.WriteLine(data);
}

Reference the ConsoleWrite method with Printer delegate

Printer<String> consoleOut = new Printer<String>(ConsoleWrite);
consoleOut("test");

 

The virtual keyword

Usage

The virtual keyword allows to modify a method, property, indexer or event declaration and to be overridden in a derived class.

Example

public virtual double volume() {
   return len * width * height
}

C# Generic Delegates

Description

These are delegates built-in with the framework and ready for your use.

List of Generic Delegates

Delegate NameArgumentsReturnsComment
Actionvoid
Action<T1,T2,T3,T4, T5,T6,T7,T8, T9,T10,T11,T12, T13,T14,T15,T16>(T1,T2,T3,T4, T5,T6,T7,T8, T9,T10,T11,T12, T13,T14,T15,T16)voidThis version of Action delegate can have 1 up to 16 arguments of different types.
Func<TResult>TResult
Func<T1,T2,T3,T4, T5,T6,T7,T8, T9,T10,T11,T12, T13,T14,T15,T16,TResult>(T1,T2,T3,T4, T5,T6,T7,T8, T9,T10,T11,T12, T13,T14,T15,T16)TResultThis version of Func delegate can have 1 up to 16 arguments of different types.
Predicate<T>Tbool

Verbatim String Literal

A double quoted string prefixed with an at (@) sign symbol will be interpreted verbatim.

Syntax

@"<STRING>"

The STRING token can have simple escaped sequences (e.g. \\ for backslash), hexadecimal escape sequence (i.e. \x<HEX_NUMBER>) and unicode escape sequences (i.e. \u<HEX_NUMBER>) that are interpreted literally. However, the "" will still be escaped an will produce a single double quote mark.

Verbatim string literal and string interpolation can be combined with the syntax as $@"<STRING>". Moreover, the {{ or }} will still be escaped.

Example

var string1 = "C:\\Windows\\System32"
var string2 = @"C:\Windows\System32"

Console.WriteLine(string1)
Console.WriteLine(string2)

The writelines will produce the same output.

String Interpolation

A double quoted string prefixed with a dollar ($) sign symbol can includes interpolation expression.

Syntax

$"<STRING>"

The STRING token can have interpolation expression and has the following syntax:

{<INTERPOLATION_EXPRESSION>[,<ALIGNMENT>][:<FORMAT_STRING>]}
Token Description
INTERPOLATION_EXPRESSION The part that will produce a formatted output.
ALIGNMENT Imposed a minimum number of character. If positive it is right-aligned and if negative it is left-aligned.
FORMAT_STRING A format that is supported by the expression. Find more on here.

If you needed to include { or } in your string output, escape it as {{ or }} respectively on which ever you need.

Example

var name = "World"
var greeting = $"Hello {name}"

Parallel Extensions

This extension calculates the most efficient way of dividing the task to the different cores available.

Note: Parallel extensions will block the calling thread.

Method Description
Parallel.For Executes for that may run in parallel.
Parallel.ForEach Executes foreach that may run in parallel.
Parallel.Invoke Executes actions that may run in parallel.

Asynchronous Programming Keywords

Async modifier

A modifier that indicates that the method can be run asynchornously.

Await keyword

Wait for a result of the asynchronous operation once the data is available without blocking the current thread. It also validates the success of the asynchronous operation. If everything is good, the execution will continue after the await keywords on its original thread.

Task.Run method

Run a task on a separate thread

ContinueWith Method

A task method that will be invoked after the running async task has returned a result.
The parameter TaskContinuationOption of this method can be use to control if the continue must be invoked if there are no exception or only with exception.

CancellationTokenSource

CancellationTokenSource is used to signal that you want to cancel an operation that utilize the token. This token be passed as a second parameter for the Task.Run method.

Dispatcher.Invoke() in WPF

Communicate with the thread that owns our UI.

ConcurrentBag Class

A thread safe collection.