Created
June 3, 2015 08:20
-
-
Save alxsimo/795c99e6635c02fa92b1 to your computer and use it in GitHub Desktop.
[C#] Extension methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string FormatString(this string format, params object[] args) | |
{ | |
return string.Format(format, args); | |
} | |
// usage | |
string message = "Welcome {0} (Last login: {1})".FormatString(userName, lastLogin); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static bool IsNull(this object source) | |
{ | |
return source == null; | |
} | |
// usage | |
public void ProcessData(DataSet input) | |
{ | |
if (!input.IsNull()) | |
{ | |
// business logic here | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Multipurpose extension methods | |
http://www.extensionmethod.net/csharp/object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static TResult NullSafe<TObj, TResult>( | |
this TObj obj, | |
Func<TObj, TResult> func, | |
TResult ifNullReturn = default(TResult)) | |
{ | |
return obj != null ? func(obj) : ifNullReturn; | |
} | |
// usage | |
foo.NullSafe(f => f.GetBar()) | |
.NullSafe(b => b.Baz) | |
.NullSafe(b => b.SomeMethod(), ifNullReturn: "whatever you would return in the null case"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Throw<TException> where TException : Exception | |
{ | |
public static void If(bool condition, string message) | |
{ | |
if (condition) | |
{ | |
throw (TException)Activator.CreateInstance(typeof(TException), | |
message); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Traverse | |
{ | |
public static IEnumerable<T> Along<T>(T node, Func<T, T> next) | |
where T : class | |
{ | |
for (var current = node; current != null; current = next(current)) | |
{ | |
yield return current; | |
} | |
} | |
} | |
// usage | |
catch (Exception ex) | |
{ | |
var sqlException = Traverse.Along(ex, e => e.InnerException) | |
.OfType<SqlException>() | |
.FirstOrDefault(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment