سؤال

What am I missing? I do not see an answer on stackoverflow, but could have missed it. It seems like the "is" operator should work (i.e. evaluate to true) for the case where

short j = 1;
int k = 2; 

if (j is int)
   Console.WriteLine("all values of j will fit into k");
else
   Console.WriteLine("all values of j will not fit into k");

I tried making the int and short nullable which did not work as well. The rationale for the conditional being true is that all values of short will fit into a variable of type int (which is potentially wrong based on the result - i.e. the "If (j is in)" evaluates to false. Thanks

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

The rationale for the conditional being true is that all values of short will fit into a variable of type int.

You're holding a paperback copy of the book The Hobbit, and someone asks you "is that thing you're holding a movie?" Do you say yes, because there's was a movie made of the book? Or do you say "no, I'm holding a paperback book, and a book is not a movie." ?

Just because there is an int that corresponds to every short does not make a short an int. The is operator tells you whether the thing you have in hand is of a particular type, hence the name "the is operator". It doesn't tell you whether there is a different thing of a different type that happens to correspond to the thing you have in hand.

short is System.Int16. int is System.Int32. These are two completely different structs.

I had created a short function that did this type of conversion. Perhaps you can extrapolate from it what you need.

private boolean IsInteger(object expression)
{
    var numericTypes = new HashSet<Type>(
                                        {
                                             typeof(Byte),
                                             typeof(SByte),
                                             typeof(Int16),
                                             typeof(UInt16),
                                             typeof(Int32),
                                             typeof(UInt32)
                                        });
    return expression != null && numericTypes.Contains(expression.GetType());
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top