The new ?? and ??? operators

Rioshin an'Harthen rharth75 at hotmail.com
Tue Sep 25 01:39:51 PDT 2007


"Janice Caron" <caron800 at googlemail.com> kirjoitti viestissä 
news:mailman.289.1190665392.16939.digitalmars-d at puremagic.com...
> On 9/24/07, Rioshin an'Harthen <rharth75 at hotmail.com> wrote:
>> I can't begin to count how many times I've written code like
>>
>>     long id = data.GetLong("ID") ?? 0;
>
> Please could you explain what that does, as I don't speak C# and can't
> make any sense of it. What is the return type of GetLong()? What is
> the type of the variable named data?

Certainly. :)

GetLong, as the name suggests, is used in this example to return a long 
(signed 64-bit value) from the database column ID, with the current database 
row referenced through the variable data (which, e.g. could be of type 
MySqlDataReader). GetLong may, if the column (in the row accessed) doesn't 
contain a value, return null, to signify this, since 0 (or any other value) 
may be a legal value in the database.

>>     long? id = data.GetLong("ID");
>
> What does the question mark after long mean? What is the type of the
> variable named id? Is is "long" or "long?" ?

The question mark after a type name means the type is nullable: basically, 
in addition to its normal range may contain the value null. A nullable type 
can be built using a struct or class, and as far as I'm able to tell from 
the standard, that's how the C# compiler creates it.

If you wish further information on nullable types, I'd recommend reading 
section 8.19 of the C# standard, available at

http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf 




More information about the Digitalmars-d mailing list