is
means "Is derived from", roughly.
Thus the following will return true.
j is short
j is object
k is int
k is object
The following will return false.
j is int
k is short
Since int
and short
do not inherit from one another in anyway.
To answer the question of "does an X fit in a Y", I do not believe there is a built in mechanism for that, since typically you need to bake the answer into your logic anyway.
If you just want to know the answer, typically C# is very good at providing implicit conversions that match your definition of is
while only providing explicit conversions otherwise.
For example you can implicitly convert an int
to a double
not because they are the same thing, but because every int
has a perfect double
representation.