NULLABLE TYPE IN C#
Presented by
MUHAMMAD ZOHAIB ANWER
FA17-BCS-105
Definition:
The Nullable type allows you to assign a null value to a
variable. Nullable types can only work with Value Type not
with Reference Type because it already contains a null value.
In C#, the compiler does not allow you to assign a null value to
a variable. So, C# 2.0 provides a special feature to assign a null
value to a variable that is known as the Nullable type.
EXPLANATION:
NULL literally means nothing - a variable that doesn't yet hold
a value. A nullable type can represent the correct range of
values for its underlying value type, plus an
additional null value. For example, Nullable<int> can be
assigned any value from -2147483648 to 2147483647, or a
null value. A nullable type has the same assignment rules as a
value type.
It must be assigned a value before using it if nullable types are
declared in a function as local variables. If it is a field of any
class then it will have a null value by default.
Characteristics of Nullable Types:
Nullable types can only be used with value types.
The Value property will throw an InvalidOperationException if value is
null; otherwise it will return the value.
The HasValue property returns true if the variable contains a value, or false
if it is null.
You can only use == and != operators with a nullable type. For other
comparison use the Nullable static class.
Nested nullable types are not allowed. Nullable<Nullable<int>> i; will give
a compile time error.
Advantage of Nullable Types:
The main use of nullable type is in database applications. Suppose,
in a table a column required null values, then you can use nullable
type to enter null values.
Nullable type is also useful to represent undefined value.
You can also use Nullable type instead of reference type to store a
null value.
IMPORTANT POINTS TO REMEMBER
• Nullable<T> type allows assignment of null to value types.
• ? operator is a shorthand syntax for Nullable types.
• Use value property to get the value of nullable type.
• Use HasValue property to check whether value is assigned to nullable
type or not.
• Static Nullable class is a helper class to compare nullable types.
CODE EXAMPLE
PROGRAM IS IN RUNNING STATE(OUTPUT)
IF I ASSIGN ANY VALUE TO NULLABLE
VARIABLE
RESULT WILL BE:
using System;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
//ASSIGNING ANY VALUE RATHER THAN NULL
int? TicketOnSale = 100;
int AvailableTicket;
if (TicketOnSale == null)
{
AvailableTicket = 0;
}
else
{
AvailableTicket = (int)TicketOnSale;
}
Console.WriteLine("AvailableTicket:{0}", AvailableTicket);
}
}
}

Nullable type in C#

  • 2.
    NULLABLE TYPE INC# Presented by MUHAMMAD ZOHAIB ANWER FA17-BCS-105
  • 3.
    Definition: The Nullable typeallows you to assign a null value to a variable. Nullable types can only work with Value Type not with Reference Type because it already contains a null value. In C#, the compiler does not allow you to assign a null value to a variable. So, C# 2.0 provides a special feature to assign a null value to a variable that is known as the Nullable type.
  • 4.
    EXPLANATION: NULL literally meansnothing - a variable that doesn't yet hold a value. A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value. A nullable type has the same assignment rules as a value type. It must be assigned a value before using it if nullable types are declared in a function as local variables. If it is a field of any class then it will have a null value by default.
  • 5.
    Characteristics of NullableTypes: Nullable types can only be used with value types. The Value property will throw an InvalidOperationException if value is null; otherwise it will return the value. The HasValue property returns true if the variable contains a value, or false if it is null. You can only use == and != operators with a nullable type. For other comparison use the Nullable static class. Nested nullable types are not allowed. Nullable<Nullable<int>> i; will give a compile time error.
  • 6.
    Advantage of NullableTypes: The main use of nullable type is in database applications. Suppose, in a table a column required null values, then you can use nullable type to enter null values. Nullable type is also useful to represent undefined value. You can also use Nullable type instead of reference type to store a null value.
  • 7.
    IMPORTANT POINTS TOREMEMBER • Nullable<T> type allows assignment of null to value types. • ? operator is a shorthand syntax for Nullable types. • Use value property to get the value of nullable type. • Use HasValue property to check whether value is assigned to nullable type or not. • Static Nullable class is a helper class to compare nullable types.
  • 8.
  • 9.
    PROGRAM IS INRUNNING STATE(OUTPUT)
  • 10.
    IF I ASSIGNANY VALUE TO NULLABLE VARIABLE
  • 11.
  • 12.
    using System; namespace ConsoleApp4 { classProgram { static void Main(string[] args) { //ASSIGNING ANY VALUE RATHER THAN NULL int? TicketOnSale = 100; int AvailableTicket; if (TicketOnSale == null) { AvailableTicket = 0; } else { AvailableTicket = (int)TicketOnSale; } Console.WriteLine("AvailableTicket:{0}", AvailableTicket); } } }