Skip to content

An extension method to check reference types in a functional-style syntax. A part of the C# Language Syntactic Sugar suite.

License

Notifications You must be signed in to change notification settings

tonygiang/CLSS.ExtensionMethods.Object.Is

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CLSS.ExtensionMethods.Object.Is

Problem

In order to make a type check during a functional-style call chain, you can do it in this way:

using Newtonsoft.Json.Linq;

var resultText = (JToken.Parse(rawJSON).SelectToken(jsonPath) is JObject)
  .ToString().ToUpper(); // resultText: TRUE/FALSE

This syntax breaks the flow of reading code from left to right and makes logical errors from one missing or one extra parenthesis easy to miss. Typical C# programmers are not used to parse parenthesis pairs as Lisp programmers are.

Solution

This package provides Is<T> extension method as a functional equivalence to the is syntax to maintain consistent LTR reading flow and be friendly to the functional syntax. The above first example can be rewritten as follows:

using CLSS.
using Newtonsoft.Json.Linq;

var resultText = JToken.Parse(rawJSON).SelectToken(jsonPath).Is<JObject>()
  .ToString().ToUpper(); // resultText: TRUE/FALSE

It also provides an equivalence to C# 7.0's convenient declaration syntax:

object greeting = "Hello, World!";
if (greeting.Is<string>(out string message))
{ ... }

Due to the limitation in the type system of C#, Is<T> is limited to using reference types. Passing a value type to the type parameter will cause a compilation error. Note that this limitation is strictly for the type parameter. You can still use value types as the caller of Is<T>.

using CLSS;

int number = 5;
var numberIsConvertible = number.Is<IConvertible>(); // true

Is<T> has some boxing overhead. It should be used if said overhead is negligible in your code path and comprehensibility is your priority.

This package is a part of the C# Language Syntactic Sugar suite.

About

An extension method to check reference types in a functional-style syntax. A part of the C# Language Syntactic Sugar suite.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages