Nullable value type idiosyncrasy

G

Guest

I came across an idiosyncrasy about nullable value types. According to the
documentation there are two valid syntax for nullable value types. One is
using the Nullable<T> generic and the other using nullable type modifier T?.
However, if you use the HasValue call without first assigning a null value to
a Nullable<T> generic instantiation you will get a compiler error "Use of
unassigned ... variable", whereas the type modifier based instantiation
doesn't give this error!
 
B

Bruce Wood

Manikkoth said:
I came across an idiosyncrasy about nullable value types. According to the
documentation there are two valid syntax for nullable value types. One is
using the Nullable<T> generic and the other using nullable type modifier T?.
However, if you use the HasValue call without first assigning a null value to
a Nullable<T> generic instantiation you will get a compiler error "Use of
unassigned ... variable", whereas the type modifier based instantiation
doesn't give this error!

How odd.

IMHO it is the first behaviour that is correct, just as it is correct
to complain that the following code refers to an unassigned variable,
even though myString can and does contain null:

string myString;
if (myString == null) ...
 
J

Jon Skeet [C# MVP]

Manikkoth said:
I came across an idiosyncrasy about nullable value types. According to the
documentation there are two valid syntax for nullable value types. One is
using the Nullable<T> generic and the other using nullable type modifier T?.
However, if you use the HasValue call without first assigning a null value to
a Nullable<T> generic instantiation you will get a compiler error "Use of
unassigned ... variable", whereas the type modifier based instantiation
doesn't give this error!

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's a sample program I've written:

using System;

class Test
{
static void Main()
{
int? x;
Nullable<int> y;
Console.WriteLine (x.HasValue);
Console.WriteLine (y.HasValue);
}
}

That complains for both x and y.
 
M

Marc Gravell

I cannot reproduce; the following (which uses both generic and specific
cases) produces (as expected) 4 x CS0165 compiler errors about unassigned
variables. Have you got an example that behaves otherwise?

Marc

static void Main() {
// test specific usage using "int" directly
int? testA;
Nullable<int> testB;
if (testA.HasValue) testA = 5;
if (testB.HasValue) testB = 5;
// test generic usage using "int" for T
Test<int>(5);
}
static void Test<T>(T a) where T : struct {
T? testA;
Nullable<T> testB;
if (testA.HasValue) testA = a;
if (testB.HasValue) testB = a;
}
 

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