What is Nullable Type in c#
Answers were Sorted based on User's Feedback
Answer / raju
Nullable type is nothing but value type cant store null values
only reference type can store null values.whenever you
assign null values to value type variable from your backend
tables at that time we use nullable.
int? x;
| Is This Answer Correct ? | 26 Yes | 2 No |
Answer / mistry
Nullable types are instances of the System.Nullable struct.
A nullable type can represent the normal range of values
for its underlying value type, plus an additional null
value. For example, a Nullable<Int32>, pronounced "Nullable
of Int32," can be assigned any value from -2147483648 to
2147483647, or it can be assigned the null value. A
Nullable<bool> can be assigned the values true or false, or
null. The ability to assign null to numeric and Boolean
types is particularly useful when dealing with databases
and other data types containing elements that may not be
assigned a value.
| Is This Answer Correct ? | 6 Yes | 0 No |
Answer / sagar
csharp provided a new concept of nullable type in which we
can store null values to integer data type , previously
like c , c++ its not possible to store null values in value
types,this languages supports to store null values only in
reference types.
| Is This Answer Correct ? | 4 Yes | 1 No |
Answer / vishnu
Declare a variable as nullable if you want to be able to
determine whether a value has been assigned. For example, if
you are storing data from a yes/no question on a form and
the use did not answer the question, you should store a null
value. The following code declares a boolean variable the
can be true, false, or null:
`VB
Dim b As Nullable(of Boolean) = Nothing
//C#
Nullable<bool> b = null;
//Shorthand notation, only for C#
bool? b = null;
Declaring a variable as nullable enables the HasValue and
Value members. Use HasValue to detect whether a value has
been set as follows:
`VB
If b.HasValue Then Console.WriteLine(“b is {0}.”, b.Value)
Else Console.WriteLine(“b is not set”);
//C#
If (b.HasValue)
Console.WriteLine(“b is {0}.”, b.Value);
Else
Console.WriteLine(“b is not set.”);
----------------------------
Exmaple:
class NullableExample
{
static void Main()
{
int? num = null;
if (num.HasValue == true)
{
System.Console.WriteLine("num = " + num.Value);
}
else
{
System.Console.WriteLine("num = Null");
}
//y is set to zero
int y = num.GetValueOrDefault();
// num.Value throws an InvalidOperationException if
num.HasValue is false
try
{
y = num.Value;
}
catch (System.InvalidOperationException e)
{
System.Console.WriteLine(e.Message);
}
}
}
| Is This Answer Correct ? | 1 Yes | 1 No |
Why do we use parameters in c#?
What is a data set in c#?
What does writeline mean?
How can you overload a method?
What is the property of a class in c#?
Why interface is required?
Can you create partial delegates and enumerations?
What is the difference between a constant and a static readonly field?
Is static thread safe?
What Is A Multicast Delegate?
Do void methods have parameters?
What is a jagged array?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)