Skip to content

Instantly share code, notes, and snippets.

@lisardggY
Last active May 16, 2019 09:15
Show Gist options
  • Save lisardggY/e38ad3461261f897a7fc to your computer and use it in GitHub Desktop.
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.
public static T As<T>(this object objectToCast)
{
return objectToCast is T typedObject ? typedObject : default;
}
@lisardggY
Copy link
Author

Updated to support value types as well.
Uses C# 7.1 syntax. Can replace default with default(T) to support C# 7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment