C# Nullable types

This is a discussion on C# Nullable types within the Framework and Interface Programming forums in category; hi, i'm a new bie to c#.net 2.0.... i've come across a new feature by name nullable types...need some info on it what is actually a null value... what exactly is advantage we get by specifying a value type as a null value... please clarify...

Go Back   Application Development Forum > Framework and Interface Programming

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 04-04-2007, 06:38 AM
=?Utf-8?B?QVZM?=
Guest
 
Default C# Nullable types

hi,
i'm a new bie to c#.net 2.0....
i've come across a new feature by name nullable types...need some info on it

what is actually a null value...
what exactly is advantage we get by specifying a value type as a null value...

please clarify
Reply With Quote
  #2  
Old 04-04-2007, 07:12 AM
Patrice
Guest
 
Default Re: C# Nullable types

For example for a price it would allow to distingusih between something that
is free (0) and something for which don't know the price (null).

It's likely you'll mostly use this for dates (if the vetn occured the date
will be filled, if the event didnt' occured date, the date will be null).

---
Patrice

"AVL" <AVL@discussions.microsoft.com> a écrit dans le message de news:
49380D43-FF22-4652-98B0-82E7C30DEC82@microsoft.com...
> hi,
> i'm a new bie to c#.net 2.0....
> i've come across a new feature by name nullable types...need some info on
> it
>
> what is actually a null value...
> what exactly is advantage we get by specifying a value type as a null
> value...
>
> please clarify



Reply With Quote
  #3  
Old 04-04-2007, 07:32 AM
Kevin Spencer
Guest
 
Default Re: C# Nullable types

First of all, a null value is nothing. That is, it indicates that a variable
or reference to data of a given type is unassigned, and therefore has no
value. As for what the advantage is, you have to understand a bit about
reference types and value types to understand nullable types. A reference
type (such as a class) is a type that is referenced by a managed pointer,
and stored in the heap. A value type (such as int, char, and array) is a
type that is referenced directly by its' actual value on the stack. So,
while a pointer can point to nothing, an integer is an allocation of 32 bits
of memory on the stack, and will always have a value, since a bit is either
a 1 or a 0.

However, a value type is actually a class that is treated like a "classic"
value type. So, it is possible to assign a null value (pointer) to a value
type, although it took some additional manipulation of the .Net Framework to
allow this behavior.

There is no advantage necessarily to nullable types, except where they may
be useful. For example, most databases these days allow a null value to be
stored for data in a table, including integers, strings, and dates. It is
helpful, when interacting with a database, to be able to assign a null value
to the variables which interact with the database.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"AVL" <AVL@discussions.microsoft.com> wrote in message
news:49380D43-FF22-4652-98B0-82E7C30DEC82@microsoft.com...
> hi,
> i'm a new bie to c#.net 2.0....
> i've come across a new feature by name nullable types...need some info on
> it
>
> what is actually a null value...
> what exactly is advantage we get by specifying a value type as a null
> value...
>
> please clarify



Reply With Quote
  #4  
Old 04-04-2007, 01:05 PM
Jon Skeet [C# MVP]
Guest
 
Default Re: C# Nullable types

Kevin Spencer <unclechutney@nothinks.com> wrote:
> First of all, a null value is nothing. That is, it indicates that a variable
> or reference to data of a given type is unassigned, and therefore has no
> value.


I'd disagree with that - being unassigned is very different to being
assigned the value null. Take:

string x;

string x = null;

(Where x is a local variable.)

In the first case, the variable is unassigned. In the second case, it
is definitely assigned with a value of null, which is the value which
doesn't refer to any object.

--
Jon Skeet - <skeet@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
Reply With Quote
  #5  
Old 04-04-2007, 01:05 PM
Jon Skeet [C# MVP]
Guest
 
Default Re: C# Nullable types

AVL <AVL@discussions.microsoft.com> wrote:
> i'm a new bie to c#.net 2.0....
> i've come across a new feature by name nullable types...need some info on it
>
> what is actually a null value...
> what exactly is advantage we get by specifying a value type as a null value...


See http://pobox.com/~skeet/csharp/csharp2/nullable.html

--
Jon Skeet - <skeet@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
Reply With Quote
  #6  
Old 04-04-2007, 01:09 PM
Scott M.
Guest
 
Default Re: C# Nullable types

I would have to disagree with your analogy.

If someting is free, it still has a value of zero dollars (as you say), but
if we don't know the price, that doesn't make the price null, it just makes
the price unkown.

null is simply a keyword that indicates that the item in question does not
have a relationship to any data at all.

A = 0 <-- A has a value of zero
A = " " <-- A has a value of the space char
A = "" <-- A has a null value (no data at all)

The benefit of null values is primarially when using databases, since most
databases have tables where not all fields are required to have a value
(like a middle name or apartment number or name suffix, such as Sr. or Jr.).
Since it is possible that a field may be null, we need a way of checking it
as such or passing null values into it.

-Scott

"Patrice" <http://www.chez.com/scribe/> wrote in message
news:uiUEhoqdHHA.1220@TK2MSFTNGP03.phx.gbl...
> For example for a price it would allow to distingusih between something
> that is free (0) and something for which don't know the price (null).
>
> It's likely you'll mostly use this for dates (if the vetn occured the date
> will be filled, if the event didnt' occured date, the date will be null).
>
> ---
> Patrice
>
> "AVL" <AVL@discussions.microsoft.com> a écrit dans le message de news:
> 49380D43-FF22-4652-98B0-82E7C30DEC82@microsoft.com...
>> hi,
>> i'm a new bie to c#.net 2.0....
>> i've come across a new feature by name nullable types...need some info on
>> it
>>
>> what is actually a null value...
>> what exactly is advantage we get by specifying a value type as a null
>> value...
>>
>> please clarify

>
>



Reply With Quote
  #7  
Old 04-04-2007, 01:16 PM
Jon Skeet [C# MVP]
Guest
 
Default Re: C# Nullable types

Scott M. <s-mar@nospam.nospam> wrote:
> I would have to disagree with your analogy.
>
> If someting is free, it still has a value of zero dollars (as you say), but
> if we don't know the price, that doesn't make the price null, it just makes
> the price unkown.
>
> null is simply a keyword that indicates that the item in question does not
> have a relationship to any data at all.


That much I'd agree with.

> A = 0 <-- A has a value of zero
> A = " " <-- A has a value of the space char
> A = "" <-- A has a null value (no data at all)


That particular example is a bit confusing, as the string literal ""
and null are very different. One is a reference to a 0-length string,
the other is a reference to no object at all.

So I'd say that "" has data - it has the data that it's an empty
string.

--
Jon Skeet - <skeet@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
Reply With Quote
  #8  
Old 04-04-2007, 02:10 PM
Patrice
Guest
 
Default Re: C# Nullable types

Not sure what you meant. How would you mark that the price is unknown if not
using a "nullable" type representing a null value ? As a side note, an empty
string is not a "null" string (in the first case we known that the value is
a zero length string, in the other case we don't know what the value is).

IMO one of the problem in discussing is that null has multiple acceptances.
In the context of a nullable type this is the same than the "NULL" (Il'l use
uppercase for this meaning) marker used in most DB, not the "null"
refererence as usual in C# (likely why MS used HasValue for what is called
"nullable" types toa void the ambiguity). VB.NET uses the Nothing keyword.

Finally I see sometimes what is IMO an abusive use of NULL. If you know that
you have no name suffix, you don't have to use a NULL value but an empty
string will do. You have to use a NULLable column if you want to distinguish
if the value is an empty string or if it has no meaning (i.e. not "known",
"applicable" or whatever semantic you attribute to the NULL value).
---
Patrice


"Scott M." <s-mar@nospam.nospam> a écrit dans le message de news:
%23NBysutdHHA.4636@TK2MSFTNGP03.phx.gbl...
>I would have to disagree with your analogy.
>
> If someting is free, it still has a value of zero dollars (as you say),
> but if we don't know the price, that doesn't make the price null, it just
> makes the price unkown.
>
> null is simply a keyword that indicates that the item in question does not
> have a relationship to any data at all.
>
> A = 0 <-- A has a value of zero
> A = " " <-- A has a value of the space char
> A = "" <-- A has a null value (no data at all)
>
> The benefit of null values is primarially when using databases, since most
> databases have tables where not all fields are required to have a value
> (like a middle name or apartment number or name suffix, such as Sr. or
> Jr.). Since it is possible that a field may be null, we need a way of
> checking it as such or passing null values into it.
>
> -Scott
>
> "Patrice" <http://www.chez.com/scribe/> wrote in message
> news:uiUEhoqdHHA.1220@TK2MSFTNGP03.phx.gbl...
>> For example for a price it would allow to distingusih between something
>> that is free (0) and something for which don't know the price (null).
>>
>> It's likely you'll mostly use this for dates (if the vetn occured the
>> date will be filled, if the event didnt' occured date, the date will be
>> null).
>>
>> ---
>> Patrice
>>
>> "AVL" <AVL@discussions.microsoft.com> a écrit dans le message de news:
>> 49380D43-FF22-4652-98B0-82E7C30DEC82@microsoft.com...
>>> hi,
>>> i'm a new bie to c#.net 2.0....
>>> i've come across a new feature by name nullable types...need some info
>>> on it
>>>
>>> what is actually a null value...
>>> what exactly is advantage we get by specifying a value type as a null
>>> value...
>>>
>>> please clarify

>>
>>

>
>



Reply With Quote
  #9  
Old 04-04-2007, 03:21 PM
Scott M.
Guest
 
Default Re: C# Nullable types

Something having an unknown value is not, in any way, related to a
dissussion of null. The very word you use "unkown" implies that there is,
in fact, something to "know". A null value indicates the exact opposite of
that, that there is no value at all. That's why I said that your alaogy was
not a good one.

When I run into situations where a value is unknown to me, I set up a
variable to capture that value. After doing that, I can then look to see if
the varialbe is null, seven, "green" or anything else. The fact that I
didn't know the value of the variable does not imply null.

You seem to be discussing what a "nullable type" is, rather than the meaning
of "null".




"Patrice" <http://www.chez.com/scribe/> wrote in message
news:esbV%23RudHHA.4188@TK2MSFTNGP02.phx.gbl...
> Not sure what you meant. How would you mark that the price is unknown if
> not using a "nullable" type representing a null value ? As a side note, an
> empty string is not a "null" string (in the first case we known that the
> value is a zero length string, in the other case we don't know what the
> value is).
>
> IMO one of the problem in discussing is that null has multiple
> acceptances. In the context of a nullable type this is the same than the
> "NULL" (Il'l use uppercase for this meaning) marker used in most DB, not
> the "null" refererence as usual in C# (likely why MS used HasValue for
> what is called "nullable" types toa void the ambiguity). VB.NET uses the
> Nothing keyword.
>
> Finally I see sometimes what is IMO an abusive use of NULL. If you know
> that you have no name suffix, you don't have to use a NULL value but an
> empty string will do. You have to use a NULLable column if you want to
> distinguish if the value is an empty string or if it has no meaning (i.e.
> not "known", "applicable" or whatever semantic you attribute to the NULL
> value).
> ---
> Patrice
>
>
> "Scott M." <s-mar@nospam.nospam> a écrit dans le message de news:
> %23NBysutdHHA.4636@TK2MSFTNGP03.phx.gbl...
>>I would have to disagree with your analogy.
>>
>> If someting is free, it still has a value of zero dollars (as you say),
>> but if we don't know the price, that doesn't make the price null, it just
>> makes the price unkown.
>>
>> null is simply a keyword that indicates that the item in question does
>> not have a relationship to any data at all.
>>
>> A = 0 <-- A has a value of zero
>> A = " " <-- A has a value of the space char
>> A = "" <-- A has a null value (no data at all)
>>
>> The benefit of null values is primarially when using databases, since
>> most databases have tables where not all fields are required to have a
>> value (like a middle name or apartment number or name suffix, such as Sr.
>> or Jr.). Since it is possible that a field may be null, we need a way of
>> checking it as such or passing null values into it.
>>
>> -Scott
>>
>> "Patrice" <http://www.chez.com/scribe/> wrote in message
>> news:uiUEhoqdHHA.1220@TK2MSFTNGP03.phx.gbl...
>>> For example for a price it would allow to distingusih between something
>>> that is free (0) and something for which don't know the price (null).
>>>
>>> It's likely you'll mostly use this for dates (if the vetn occured the
>>> date will be filled, if the event didnt' occured date, the date will be
>>> null).
>>>
>>> ---
>>> Patrice
>>>
>>> "AVL" <AVL@discussions.microsoft.com> a écrit dans le message de news:
>>> 49380D43-FF22-4652-98B0-82E7C30DEC82@microsoft.com...
>>>> hi,
>>>> i'm a new bie to c#.net 2.0....
>>>> i've come across a new feature by name nullable types...need some info
>>>> on it
>>>>
>>>> what is actually a null value...
>>>> what exactly is advantage we get by specifying a value type as a null
>>>> value...
>>>>
>>>> please clarify
>>>
>>>

>>
>>

>
>



Reply With Quote
  #10  
Old 04-04-2007, 05:36 PM
Kevin Spencer
Guest
 
Default Re: C# Nullable types

I'll have to take issue with you here, Jon. Null is not a value technically
speaking, as a value is something. If you declare a string variable without
initializing it, and test it for null, it will return true. While null
doesn't have exactly the same technical meaning that it used to have back in
the days of "simple" C programming, it still signifies nothing, and an
unassigned variable is nothing. You can't make a distinction between nothing
and nothing, and the compiler doesn't either. There are situations in which
the compiler will insist that you assign a null value to a variable, but
those are "safety" checks, and it isn't consistent, as there are other times
when it will not, and will test true for null with an unassigned variable.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.207deb14e34b931698da81@msnews.microsoft.c om...
> Kevin Spencer <unclechutney@nothinks.com> wrote:
>> First of all, a null value is nothing. That is, it indicates that a
>> variable
>> or reference to data of a given type is unassigned, and therefore has no
>> value.

>
> I'd disagree with that - being unassigned is very different to being
> assigned the value null. Take:
>
> string x;
>
> string x = null;
>
> (Where x is a local variable.)
>
> In the first case, the variable is unassigned. In the second case, it
> is definitely assigned with a value of null, which is the value which
> doesn't refer to any object.
>
> --
> Jon Skeet - <skeet@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



Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:44 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.