Last active
May 16, 2019 09:15
-
-
Save lisardggY/e38ad3461261f897a7fc to your computer and use it in GitHub Desktop.
For casting an object as part of a Fluent expression without having to put annoying parentheses around casting expressions.
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 T As<T>(this object objectToCast) | |
{ | |
return objectToCast is T typedObject ? typedObject : default; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to support value types as well.
Uses C# 7.1 syntax. Can replace
default
withdefault(T)
to support C# 7.0.