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 .

Stephan B
  • 837
  • 1
  • 16
  • 41
MSU
  • 415
  • 3
  • 6
  • 13

5 Answers5

21

You have to declare it as a nullable type:

double? x;
x = null;

Non nullable types like double can not be null

TGH
  • 38,769
  • 12
  • 102
  • 135
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;
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
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?

Community
  • 1
  • 1
internals-in
  • 4,798
  • 2
  • 21
  • 38
0

Short answer: You can't.

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

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
0
use 
double? x;

and assign null value to x variable like

x = null;
Rahul
  • 5,603
  • 6
  • 34
  • 57