C# extensions for everyday usage
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE
README.md

README.md

EverydayExtensionsCsharp

This project contains examples of the C# extensions for everyday usage. I am using them in each of my projects and happy about it :)

Collection extensions

public static List<T> NullToEmpty<T>(this List<T> source)
{
    return source ?? new List<T>();
}
public static T[] ExpandArray<T>(this T[] array, T item)
{
    if (array == null || array.Length == 0)
    {
        array = new[] { item };
        return array;
    }

    var expandedArray = new T[array.Length + 1];
    Array.Copy(array, expandedArray, array.Length);
    expandedArray[array.Length] = item;

    return expandedArray;
}

String's extensions

public static bool IsNullOrEmpty(this string value)
{
    return string.IsNullOrEmpty(value);
}
public static bool NotNullOrEmpty(this string value)
{
    return !string.IsNullOrEmpty(value);
}

Json helpers (JSON.NET is required)

public static string ToJson<T>(this T obj)
{
    return JsonConvert.SerializeObject(obj, Formatting.Indented);
}

HttpRequest

public static Encoding GetEncodingFromRequest(this HttpRequest request)
{
    try
    {
        var contentTypeHeader = request.Headers["Content-Type"];
        if (!contentTypeHeader.Any())
            return Encoding.UTF8;

        var contentType = new ContentType(contentTypeHeader);
        var charset = contentType.CharSet ?? "UTF-8";

        return Encoding.GetEncoding(charset);
    }
    catch
    {
        return null;
    }
}

Entity Framework

public static IQueryable<TEntity> Includes<TEntity>(this IQueryable<TEntity> source, 
    params Expression<Func<TEntity, object>>[] includes)
    where TEntity : class
{
    foreach (var include in includes)
    {
        source = source.Include(include);
    }
    
    return source;
}