15

Such as i have declared,

double x;

now i want to assign

x=NULL how can i do this ? I have seen some other answers of it but couldn't understand them, that's why opening this thread .

5 Answers 5

21

You have to declare it as a nullable type:

double? x;
x = null;

Non nullable types like double can not be null

21

You cant assign null to a non-nullable type

You can use NaN (not a number) for a non-nullable double

double x;
x = double.NaN;
1

Nullable types are instances of the System.Nullable(Of T) struct. A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, a Nullable, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable can be assigned the values true false, or null. The ability to assign null to numeric and Boolean types is especially useful when you are dealing with databases and other data types that contain elements that may not be assigned a value. For example, a Boolean field in a database can store the values true or false, or it may be undefined.

double? amount = null;

http://msdn.microsoft.com/en-us/library/vstudio/2cf62fcy.aspx

Possible Duplicates:

Why can't I set a nullable int to null in a ternary if statement?
Nullable types and the ternary operator: why is `? 10 : null` forbidden?

0

Short answer: You can't.

A double can only contain a valid double-precision floating point value.

0
0
use 
double? x;

and assign null value to x variable like

x = null;
2
  • 1
    same answer posted by @TGH
    – Niventh
    Mar 19, 2013 at 5:16
  • oh yes i haven't read this, but i think answer is correct and it's same ofcourse..
    – Rahul
    Mar 19, 2013 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.