How

Context

Get current AppDomain

AppDomain.CurrentDomain

Path

Get the full path of the parent folder from a path name

Path.GetDirectoryName(fileName)

Get the parent folder name from a path

Path.GetDirectoryName(fileName).Split('\\').Last()

Get the file name from a path (absolute or relative)

Path.GetFileName(fileName)

fileName.Split('\\').Last()

Compress / Decompress

How to: Compress and Extract Files

Creating Zip archives in .NET

System.IO.Compression

ZipPackage

.NET Framework Zip / UnZip Tool Using the Packaging Namespace

SharpZipLib

DotNetZip

Reflection

GetMemberName()

public static string GetMemberName<T>(Expression<Func<T>> memberExpression)
{
    MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
    return expressionBody.Member.Name;
}

After C# 6, just use nameof operator.

Sequences Generator

Number Sequences

Enumerable.Range

Generates a sequence of integral numbers within a specified range.

var seq = Enumerable.Range(0, 10);
// Generate a sequence of integers from 1 to 10 
// and then select their squares.
IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x);

Interlocked

using System.Threading;

private static int orderNumber = 0;

int Seq()
{
    return Interlocked.Increment(ref orderNumber);
}

All Types of Sequences

Enumerable.Range

IEnumerable<string> orderNumbers = 
Enumerable.Range(1, 10).Select(x =>  string.Format("Order{0}", x.ToString().PadLeft(9, '0')));

yield

IEnumerable<int> Sequence(int n)
{
    while (true)
    {
        yield return  n++;
    }
}

Enumerable.Repeat

Generates a sequence that contains one repeated value.

IEnumerable<string> strings = Enumerable.Repeat("I like programming.", 15);

results matching ""

    No results matching ""