more about nullable types where the Value property is used

T

Tony Johansson

Hello!

What purpose has this Value propery on nullable types because it possible
to display their value without using it.
For example:

int? intNullable1 = 3;
int? intNullable2 = 5;
Console.WriteLine(result); // This display 8
Console.WriteLine(result.Value); // This also display 8 the same

So I can't see any point in using this Value property on nullable types or
have I missed something here perhaps
that makes the Value property useful to have?

//Tony
 
L

Lasse Vågsæther Karlsen

Tony said:
Hello!

What purpose has this Value propery on nullable types because it possible
to display their value without using it.
For example:

int? intNullable1 = 3;
int? intNullable2 = 5;
Console.WriteLine(result); // This display 8
Console.WriteLine(result.Value); // This also display 8 the same

So I can't see any point in using this Value property on nullable types or
have I missed something here perhaps
that makes the Value property useful to have?

//Tony

try this:

public void Test(Int32 value)
{
}

Test(intNullable1);
Test(intNullable1.Value);
 
R

Rene

The code:

Console.WriteLine(result);

Is calling the "ToString()" method on the "result" variable. So its
equivalent of you saying:

Console.WriteLine(result.ToString());

Now, if you look at the internal implementation of the "ToString()" method
on the "Nullable" variable type (using Reflector) is implemented the
following way:

public override string ToString()
{
if (!this.HasValue)
{
return "";
}
return this.value.ToString();
}

So as you can see, you were calling the "Value" property, you just didn't
know you were doing it.
 

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

Top