null on SqlString

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

I'm wondering why the following code do not throw a null exception?

In the third line I set s to null (make it to point nowhere), however later
I can call it's IsNull property.

AFAIK, if I set a reference-type variable to null, I cannot call anything on
that.

Can somebody explain this? (It can be technical as well)
Thanks,

//////////
SqlString s = new SqlString();
s = "Dummy";
s = null;

if (s.IsNull == true)
{
Console.WriteLine("s is null");
}
else
{
Console.WriteLine("s is not null");
}
//////////
 
John Smith said:
In the third line I set s to null (make it to point nowhere), however later
I can call it's IsNull property.

When the code is written as,

s = null;

There is actually a type conversion that is performed that looks like:

s.Value = SqlString.Null;
AFAIK, if I set a reference-type variable to null, I cannot call anything on
that.

SqlString turns out to not be a reference type. It is a value type, therefore
it exists on the stack as soon as it is declared until it passes out of scope.


Derek Harmon
 
Back
Top