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
 

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

Back
Top