How to Work with Nullable Types in C#

How to Work with Nullable Types in C#

Nullable is a term in C# that allows an extra value null to be owned by a form. We will learn in this article how to work with Nullable types in C#.

There are majorly two types of data types in C# Value and Reference type. We can not assign a null value directly to the Value data type, therefore, C# 2.0 provides us the Nullable types to assign a value data type to null.

What is Nullable types? the Nullable types used to assign the null value to the value data type. That means we can directly assign a null value to a value data type attribute. Using Nullable, we can declare a null value where T is a type like int, float, bool, etc.

Nullable types represent the Null value along with the actual range of that data type. Like the int data type can hold the value from -2147483648 to 2147483647 but a Nullable int can hold the value null and range from -2147483648 to 2147483647

How to declare Nullable types There are two ways to declare Nullable types.

Nullable example;


int? Example;

Properties of Nullable types Nullable types have two properties.

  • HasValue
  • Value HasValue: This property returns a value of bool depending on whether or not the nullable variable has a value. If the variable has a value, it returns true; otherwise, if it has no value or is null, it returns false.

Nullable a = null; Console.WriteLine(a.HasValue); // Print False Nullable b = 9; Console.WriteLine(b.HasValue); // Print True

Value: The value of the variable Nullable form is given by this property. If the variable has any value, the value will be returned; else, it will give the runtime InvalidOperationException exception when the variable value is null.

Nullable a = null;

Console.WriteLine(a.Value); // Gives run time exception of type 'InvalidOperationException'

Nullable b = 9;

Console.WriteLine(b.Value); // Print 9

You can read more about method of Nullable types and rules of using Nullable types in this blog here:

programming technology coding programminglanguage javascript python

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Guide to Python Programming Language

Guide to Python Programming Language

Generate QR Code using Python — Python Programming — PyShark

This article will explore how to generate QR code in Python and some useful creation features from pyqrcode library. QR codes recently became more popular than ever before, yet few people know that the first iterations of QR codes were created back in 1990s in Japan for the automotive industry.

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners