views:

152

answers:

4

When I call a GetType on a int- or a DateTime-property, I get the expected results, but on a string-property, I get a NullReferenceException (?) :

private int      PropInt    { get; set; }
private DateTime PropDate   { get; set; }
private string   propString { get; set; }

WriteLine(PropInt.GetType().ToString());    // Result : System.Int32
WriteLine(PropDate.GetType().ToString());   // Result : System.DateTime
WriteLine(propString.GetType().ToString()); // Result : NullReferenceException (?!)

Can someone explain how come ? In what way differs a string-prop from a int-prop ?

+8  A: 

If the property's value is null, then you'll get a NullReferenceException when trying to access object methods or properties, like GetType(). Primitive types like int and DateTime are value types, and as such cannot hold a null value, which is why GetType() won't fail any more than any of their other member functions.

Adam Robinson
+2  A: 

Because string is a reference type where the others are not. The DateTime and the Int have to have values by default, they can't be null.

What you have to understand is the the compiler is creating a variable for you to store the information. In C# 3.0 you don't have to explicitly declare it, but it is still there, so it's creating a DateTime variable and an int variable and initializing them to their default values so not to cause a compiler error. With a string, it doesn't need to do this (initialize a default value), because it's a reference type.

Kevin
-1...global variable? There is no such concept in .NET, and what would its purpose be here?
Adam Robinson
you're right, I mispoke.
Kevin
Gotcha. Downvote removed!
Adam Robinson
+1  A: 

Initial value of propString is null. We cann't execute method of null. If you initialaze propString: propString = "" then you can execute GetType() without Exception

Code without exception:

private int      PropInt    { get; set; }
private DateTime PropDate   { get; set; }
private string   propString { get; set; }

propString = ""; // propString != null

WriteLine(PropInt.GetType().ToString());    // Result : System.Int32
WriteLine(PropDate.GetType().ToString());   // Result : System.DateTime
WriteLine(propString.GetType().ToString()); // Result : System.String
DreamWalker
+2  A: 

To emphasize what the other answers have indicated, change int to int? and DateTime to DateTime? and try running the code again. Since those values can now hold nulls, you will get the same exception.

Bomlin
Actually this is incorrect. While you can assign a `Nullable<T>` to `null` and compare it to `null`, *the value is not actually `null`*. `Nullable<T>` is a `struct` and has to follow all value type conventions, such as not being able to store `null`. The assignment and comparison is just syntactic sugar for the parameterless constructor and for checking the `HasValue` property, respectively.
Adam Robinson
No; Bomlin is correct; an uninitialised `Nullable<T>` throws an exception on `GetType()`; see here for why: http://stackoverflow.com/questions/194484/whats-the-strangest-corner-case-youve-seen-in-c-or-net/194671#194671
Marc Gravell