0

Possible Duplicate:
Type Checking: typeof, GetType, or is?

What is the difference between the following:

bool isLong (object a){
    return (a.GetType()==typeof(INT64));
}

bool isLong (object a){
    return (typeof(a)==typeof(INT64));
}

bool isLong (object a){
    return (a is INT64);
}

In addition. there is an option that a will be null. Does those functions can handle such case?

( I need to convert "value" to long? for example: c.CustomerId = (long?)value; )

can I use: long? id=(a as long?)

Community
  • 1
  • 1
Naor
  • 23,465
  • 48
  • 152
  • 268

1 Answers1

1

The first uses the instance a and will throw an exception if a is null. It compares the type using the Type class.

The second way won't work because typeof only works with type names, not instances.

In the last way, you are saying a is of type INT64. It not throw an exception if a is null. This is the preferred method.

However in your case, the conversion you could do

c.CustomerId = value as long?;
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445