The is keyword

T

Tony Johansson

Hello!

I just want to check with you the usage of the is keyword is only relevant
when having reference type.

Is that correct?

//Tony
 
J

Jon Skeet [C# MVP]

I just want to check with you the usage of the is keyword is only relevant
when having reference type.

Is that correct?

Well, the expression on the left has to be a reference type, but the
type name on the right could be a value type when it comes to boxing:

object o = 10;
if (o is int)
{
// Yup
}

Jon
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello!

I just want to check with you the usage of the is keyword is only relevant
when having reference type.

Is that correct?

//Tony

the left side at least, you can do object1 is Int32.

You will fnid a different situation with the "as" operator, there the
second operator should be a reference type.
so this expression is valid:

int i = 23;
object e = i as object;
 
M

Marc Gravell

Well, the expression on the left has to be a reference type

It doesn't have to be... of course the compiler knows that structs can't
be subclassed, so will raise warnings about unnecessary usage, but
consider generics:

static int? CompareToDefault<T>(T value) where T : struct
{
if (value is IComparable<T>)
{
return Comparer<T>.Default.Compare(default(T), value);
}
else
{
return null;
}
}

The left hand operand is definitely a value-type...

Marc
 
J

Jon Skeet [C# MVP]

It doesn't have to be... of course the compiler knows that structs can't
be subclassed, so will raise warnings about unnecessary usage, but
consider generics:

Ooh, very true. Interesting example :)

Can you think of any non-generic value type examples? I suspect
*those* are impossible, because other than generic cases, if the
compiler knows it's a value type, it knows the exact type. I could be
missing something though.

Jon
 
M

Marc Gravell

Well, you can still *use* is with known value-types; but the compiler
will ignore you ;-p But it is a warning, not an error...

But no, I can't think of any useful, non-generic value-type examples of
"is".

Marc
 
H

Hans Kesting

It happens that Ignacio Machin ( .NET/ C# MVP ) formulated :
the left side at least, you can do object1 is Int32.

You will fnid a different situation with the "as" operator, there the
second operator should be a reference type.
so this expression is valid:

int i = 23;
object e = i as object;

Not quite, a nullable type will work as well. The main point is that
"null" should be an acceptable value for the receiving type.

object o = "nop";
int? i = o as int?; // i becomes null

or you could use
int i2 = o as int? ?? 2; // i2 becomes 2

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

about null values 8
terminologi instaniating 2
ConnectionPool 1
Reserved Keyword 4
new keyword in 'hide' base class member 3
unsafe and GC 5
instantiating string 9
Keywords used in C# at this site 1

Top