top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to use nullable types in .Net?

+4 votes
431 views
How to use nullable types in .Net?
posted Nov 28, 2014 by Manikandan J

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Value types can take either their normal values or a null value. Such types are called nullable types.

[csharp]Int? someID = null;
If(someID.HasVAlue)
{
}
[/csharp]
answer Dec 1, 2014 by Shivaranjini
0 votes

Nullable types allow you to create a value type variable that can be marked as valid or invalid so that you can make sure a variable is valid before using it. Regular value types are called non-nullable types.

The important things you need to know about nullable type conversions are the following:

• There is an implicit conversion between a non-nullable type and its nullable version. That is, no cast is needed.
• There is an explicit conversion between a nullable type and its non-nullable version.

For example, the following lines show conversion in both directions. In the first line, a literal of type
int is implicitly converted to a value of type int? and is used to initialize the variable of the nullable type.
In the second line, the variable is explicitly converted to its non-nullable version.

int? myInt1 = 15; // Implicitly convert int to int?
int regInt = (int) myInt1; // Explicitly convert int? to int

int? myInt1 = 15; // Implicitly convert int to int?
int regInt = (int) myInt1; // Explicitly convert int? to int

answer May 9, 2017 by Jdk
...