473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nullable value type idiosyncrasy


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<Tgeneric and the other using nullable type modifier T?.
However, if you use the HasValue call without first assigning a null value to
a Nullable<Tgeneric instantiation you will get a compiler error "Use of
unassigned ... variable", whereas the type modifier based instantiation
doesn't give this error!
Sep 13 '06 #1
3 1320

Manikkoth wrote:
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<Tgeneric and the other using nullable type modifier T?.
However, if you use the HasValue call without first assigning a null value to
a Nullable<Tgeneric 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) ...

Sep 14 '06 #2
Manikkoth <Ma*******@discussions.microsoft.comwrote:
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<Tgeneric and the other using nullable type modifier T?.
However, if you use the HasValue call without first assigning a null value to
a Nullable<Tgeneric 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<inty;
Console.WriteLine (x.HasValue);
Console.WriteLine (y.HasValue);
}
}

That complains for both x and y.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 14 '06 #3
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<inttestB;
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<TtestB;
if (testA.HasValue) testA = a;
if (testB.HasValue) testB = a;
}
Sep 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Wood | last post by:
I was just looking at an article about using nullable value types. (value types that can effectively have no value and not be set). The syntax is to append a question-mark to the value type in...
4
by: ESPNSTI | last post by:
Hi, Please don't shoot me if the answer is simple, I'm new to .Net and C# :) .. I'm attempting to convert a nullable type to it's "non-nullable" type in a generic way (without knowing what...
3
by: rubikzube* | last post by:
Hi. I'm trying to write a snippet of code to detect if a variable is a nullable<t> struct, and I can't figure out the right syntax or if such a thing is even possible... Below are the results...
1
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
5
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
2
by: Tony Johansson | last post by:
Hello! These two declarations(1 and 2) are the same I assume. 1. System.Nullable<intnullable; 2. System.Nullable<intnullable = new System.Nullable<int(); So because these 1 and 2 are the...
6
by: Tony Johansson | last post by:
Hello! I'm reading in a book called Visual C# 2005. In the chapter about Generics there ia a section about Nullable types. Here is the text that isn't complete true I think. It says: "You...
8
by: Tony Johansson | last post by:
Hello! Jon skeet answer this question in a previous mail for several days ago if nullable type is a reference or a value type? With the following answer. It's a struct - otherwise there'd be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.