SlideShare a Scribd company logo
Nullable type in C#
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);
}
}
}

More Related Content

PPT
Java interfaces & abstract classes
PPTX
Arrays in Java
PPTX
collection framework in java
PPTX
Packages in java
PPTX
Java swing
PPTX
Abstract Class Presentation
PPTX
Exception Handling in Java
PPT
Exception Handling in JAVA
Java interfaces & abstract classes
Arrays in Java
collection framework in java
Packages in java
Java swing
Abstract Class Presentation
Exception Handling in Java
Exception Handling in JAVA

What's hot (20)

PPTX
Java interface
PPTX
Polymorphism presentation in java
PPT
Types of exceptions
PPT
Interface in java By Dheeraj Kumar Singh
PPTX
Interfaces in java
PPTX
Interfaces c#
PPT
Networking & Socket Programming In Java
PPT
C#.NET
PDF
Java collections
PPTX
Control flow statements in java
PPTX
PPT
ASP.NET MVC Presentation
PPTX
Dependency injection presentation
PPTX
Introduction to java
PPTX
Event Handling in java
PDF
Lambda Expressions in Java
ODP
Routing & Navigating Pages in Angular 2
PPTX
Basics of JAVA programming
PPTX
Methods in java
Java interface
Polymorphism presentation in java
Types of exceptions
Interface in java By Dheeraj Kumar Singh
Interfaces in java
Interfaces c#
Networking & Socket Programming In Java
C#.NET
Java collections
Control flow statements in java
ASP.NET MVC Presentation
Dependency injection presentation
Introduction to java
Event Handling in java
Lambda Expressions in Java
Routing & Navigating Pages in Angular 2
Basics of JAVA programming
Methods in java
Ad

Similar to Nullable type in C# (20)

PDF
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
PDF
Swift Tutorial Part 2. The complete guide for Swift programming language
PPTX
Data types in java.pptx power point of java
ODP
Shapeless- Generic programming for Scala
PDF
Review of c_sharp2_features_part_iii
PPTX
Kotlin in action
PPTX
data handling revision.pptx
PPTX
typescript.pptx
PPTX
C# slid
PPTX
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
PPTX
Typescript: Beginner to Advanced
PPTX
Lecture 4: Functions
PDF
Java+8-New+Features.pdf
PDF
Introduction to Javascript and Typescript.pdf
PDF
data types.pdf
PDF
PPTX
Virtual function and abstract class
PPTX
Day 2 Compose Camp.pptx
PDF
From DOT to Dotty
PDF
7. VARIABLEs presentation in java programming. Pdf
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 2. The complete guide for Swift programming language
Data types in java.pptx power point of java
Shapeless- Generic programming for Scala
Review of c_sharp2_features_part_iii
Kotlin in action
data handling revision.pptx
typescript.pptx
C# slid
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
Typescript: Beginner to Advanced
Lecture 4: Functions
Java+8-New+Features.pdf
Introduction to Javascript and Typescript.pdf
data types.pdf
Virtual function and abstract class
Day 2 Compose Camp.pptx
From DOT to Dotty
7. VARIABLEs presentation in java programming. Pdf
Ad

Recently uploaded (20)

PDF
AlphaEarth Foundations and the Satellite Embedding dataset
PDF
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
PDF
Cosmic Outliers: Low-spin Halos Explain the Abundance, Compactness, and Redsh...
PDF
Biophysics 2.pdffffffffffffffffffffffffff
PDF
. Radiology Case Scenariosssssssssssssss
PPTX
2Systematics of Living Organisms t-.pptx
PPTX
ECG_Course_Presentation د.محمد صقران ppt
PPTX
TOTAL hIP ARTHROPLASTY Presentation.pptx
PPT
POSITIONING IN OPERATION THEATRE ROOM.ppt
PPTX
7. General Toxicologyfor clinical phrmacy.pptx
DOCX
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
PPT
protein biochemistry.ppt for university classes
PPTX
famous lake in india and its disturibution and importance
PPTX
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
PPTX
The KM-GBF monitoring framework – status & key messages.pptx
PDF
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
PPT
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
PPTX
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
PPTX
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
PPTX
INTRODUCTION TO EVS | Concept of sustainability
AlphaEarth Foundations and the Satellite Embedding dataset
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
Cosmic Outliers: Low-spin Halos Explain the Abundance, Compactness, and Redsh...
Biophysics 2.pdffffffffffffffffffffffffff
. Radiology Case Scenariosssssssssssssss
2Systematics of Living Organisms t-.pptx
ECG_Course_Presentation د.محمد صقران ppt
TOTAL hIP ARTHROPLASTY Presentation.pptx
POSITIONING IN OPERATION THEATRE ROOM.ppt
7. General Toxicologyfor clinical phrmacy.pptx
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
protein biochemistry.ppt for university classes
famous lake in india and its disturibution and importance
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
The KM-GBF monitoring framework – status & key messages.pptx
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
INTRODUCTION TO EVS | Concept of sustainability

Nullable type in C#

  • 2. NULLABLE TYPE IN C# Presented by MUHAMMAD ZOHAIB ANWER FA17-BCS-105
  • 3. 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.
  • 4. 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.
  • 5. 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.
  • 6. 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.
  • 7. 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.
  • 9. PROGRAM IS IN RUNNING STATE(OUTPUT)
  • 10. IF I ASSIGN ANY VALUE TO NULLABLE VARIABLE
  • 12. 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); } } }