Skip to content
A collection of useful small extension methods
Branch: master
Clone or download
Latest commit 3aa1777 Jun 24, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Projects/Clave.ExtensionMethods Fixes #6 Jun 24, 2019
Tests/Clave.ExtensionMethods.Tests Fixed #4 Jun 24, 2019
.gitignore initial commit Jan 18, 2019
Clave.ExtensionMethods.sln initial commit Jan 18, 2019
GitVersion.yml initial commit Jan 18, 2019
License.md initial commit Jan 18, 2019
Readme.md Added tests and documentation Jan 18, 2019
appveyor.yml initial commit Jan 18, 2019

Readme.md

Clave.ExtensionMethods

This is a collection of small and useful extension methods for C#.

To use it just add using Clave.ExtensionMethods; at the top of the file

String extensions

Join

Join an enumerable of strings

var strings = new[] {"a", "b", "c"};
strings.Join(" "); // "a b c"

ConcatWithSpace

Concatenate strings using space, triming away extra spaces

"a".ConcatWithSpace("b", "c"); // "a b c"
"a".ConcatWithSpace(" b ", " ", "c"); // "a b c"

ToNullIfEmpty

Converts a string to null if it is empty, useful together with the ?? operator

someString.ToNullIfEmpty() ?? "defaultValue";

ToNullIfWhiteSpace

Converts a string to null if it is empty or only contains white space characters, useful together with the ?? operator

someString.ToNullIfWhiteSpace() ?? "defaultValue";

SkipPrefix

Removes a prefix of a string if it is present

"something".SkipPrefix("some"); // "thing"

ToInt

Converts a string to an int. If it fails it returns the fallback value, which defaults to 0.

"123".ToInt(); // 123
"foo".ToInt(); // 0
"foo".ToInt(-1); // -1

ToDecimal

Converts a string to an decimal. If it fails it returns the fallback value, which defaults to 0.

"123.45".ToDecimal(); // 123.45
"foo".ToDecimal(); // 0
"foo".ToDecimal(-1); // -1

Enumerable extensions

Only

Creates an enumerable with only one value

var enumerable = "something".Only();

And

Creates an enumerable of one or more values

var enumerable = "some".And("thing", "else");

NotAny

Returns true if the list is empty, or if nothing matches the predicate

new string[0].NotAny(); // true
new []{"a", "b", "c"}.NotAny(string.IsNullOrEmpty); // true

WhereNot

Returns only the items that don't match the predicate

new []{"a", "", "c"}.WhereNot(string.IsNullOrEmpty); // "a", "c"

WhereNotNull

Returns only the items that are not null

new []{"a", null, "c"}.WhereNotNull(); // "a", "c"

// this also works with a predicate

new []{new Foo("a"), new Foo(null), new Foo("c")}.WhereNotNull(f => f.Value); // "a", "c"

ToReadOnlyCollection

Returns an IReadOnlyCollection, does nothing if the type is already an IReadOnlyCollection

"a".And("b", "c").ToReadOnlyCollection();
new []{"a", "b", "c"}.ToReadOnlyCollection();

ToReadOnlyList

Returns an IReadOnlyList, does nothing if the type is already an IReadOnlyList

"a".And("b", "c").ToReadOnlyList();
new []{"a", "b", "c"}.ToReadOnlyList();

DistinctBy

Returns only the entries with a distinct key

new []{new Foo("a"), new Foo("a"), new Foo("c")}.DistinctBy(f => f.Value); // "a", "c"

Empty

This is a useful way to get empty immutable objects. Since they are immutable the same empty instance can be reused every time.

ReadOnlyCollection

Returns an empty IReadOnlyCollection

Empty.ReadOnlyCollection<string>();

ReadOnlyList

Returns an empty IReadOnlyList

Empty.ReadOnlyList<string>();

Array

Returns an empty Array

Empty.Array<string>();

Enum

ToEnum

Converts a string to an enum, throws exception if it is an unknown string

"Monday".ToEnum<Weekday>(); // Weekday.Monday

ToEnumOrDefault

Converts a string to an enum, uses the default value if it is an unknown string

"Monday".ToEnumOrDefault(Weekday.Unknown); // Weekday.Monday
"BlaBla".ToEnumOrDefault(Weekday.Unknown); // Weekday.Unknown

Magic

These are magical extension methods that allow you to do useful magic.

To use it just add using Clave.ExtensionMethods.Magic; at the top of the file

Add list to list

This will add all the items in a list to another list

var list = new List<string>
{
	"some",
	GetStringList(),
	"thing"
}

Await many things

This lets you await several tasks without using Task.WhenAll

await DoSomethingAsync().And(DoSomethingElseAsync(), AtTheSameTimeAsync());

License

The MIT license

You can’t perform that action at this time.