C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Nullable

In C#, nullable types are used to represent value types that can be assigned a value or set to null. This can be useful when you want to indicate that a value is missing or undefined. In this tutorial, we'll cover the following topics related to nullable types in C#:

  • Using nullable value types
  • The Nullable<T> structure
  • The null-coalescing operator
  • Nullable reference types (C# 8.0 and later)

Let's begin!

  • Using nullable value types

To create a nullable value type, use the ? modifier on a value type:

int? nullableInt = null;

You can also use the Nullable<T> structure, where T is the underlying value type:

Nullable<int> nullableInt = null;

You can assign a value to a nullable type just like any other value type:

int? nullableInt = 42;

To get the value of a nullable type, use the Value property:

int? nullableInt = 42;
int intValue = nullableInt.Value; // intValue is now 42

To check if a nullable type has a value, use the HasValue property:

int? nullableInt = null;
bool hasValue = nullableInt.HasValue; // hasValue is now false
  • The Nullable<T> structure

The Nullable<T> structure is a generic structure used to create nullable value types. It provides the following properties:

  • HasValue: A boolean indicating whether the nullable object has a value.
  • Value: The value of the nullable object if it has a value; otherwise, it throws an InvalidOperationException.
  • The null-coalescing operator

The null-coalescing operator (??) is used to provide a default value when a nullable type is null. It returns the left-hand operand if it has a value; otherwise, it returns the right-hand operand:

int? nullableInt = null;
int intValue = nullableInt ?? 0; // intValue is now 0, because nullableInt is null
  • Nullable reference types (C# 8.0 and later)

Starting with C# 8.0, you can use nullable reference types to indicate that a reference type can be null. By default, reference types are non-nullable, which means they can't be assigned a null value. To create a nullable reference type, use the ? modifier:

string? nullableString = null;

To enable nullable reference types for your project, add the following line to your .csproj file:

<PropertyGroup>
    <Nullable>enable</Nullable>
</PropertyGroup>

That's it! You now have a basic understanding of how to use nullable types in C#. Nullable types provide a way to represent value types that can have a value or be null, making it easier to handle missing or undefined values in your code.

  1. How to use nullable types in C#

    Nullable types allow variables to have a value or be null. They are defined by appending ? to the value type.

    int? nullableInt = null;
    
  2. Nullable value types in C#

    Nullable value types extend value types to allow for a null value.

    int? nullableInt = null;
    
  3. C# nullable types vs. non-nullable types

    • Nullable Types:

      int? nullableInt = null;
      
    • Non-Nullable Types:

      int nonNullableInt = 42;
      
  4. Nullable reference types in C#

    Nullable reference types are introduced to help avoid null-reference exceptions. Enable them in your project settings.

    #nullable enable
    string? nullableString = null;
    
  5. Nullable types and database interactions in C#

    Nullable types are useful when working with databases where a field can be null.

    int? databaseValue = ReadFromDatabase();
    
  6. Working with nullables in C# classes

    Nullable types can be used as members in classes:

    class MyClass
    {
        public int? NullableInt { get; set; }
    }
    
  7. Nullable types and LINQ in C#

    Use nullable types with LINQ to handle potential null values from queries:

    var result = list.Where(item => item?.Value > 5).ToList();
    
  8. Nullable types and conditional statements in C#

    Handle nullable types in conditional statements:

    int? nullableValue = GetValue();
    if (nullableValue.HasValue)
    {
        // Process non-null value
    }
    else
    {
        // Handle null case
    }
    
  9. Using Nullable<T> struct in C#

    The Nullable<T> struct provides methods for working with nullable types:

    int? nullableInt = null;
    Nullable<int> anotherNullableInt = Nullable<int>.FromNullable(nullableInt);
    
  10. C# nullable types in method parameters

    Method parameters can be nullable:

    void ProcessValue(int? nullableValue)
    {
        // Process nullableValue
    }
    
  11. Handling nullables in C# expressions

    Handle nullable types in expressions using the null-conditional operator (?.) and null-coalescing operator (??):

    int? nullableInt = GetNullableValue();
    int result = nullableInt?.GetValueOrDefault() ?? 0;
    
  12. Nullable types in C# and the null-coalescing operator

    The null-coalescing operator (??) provides a concise way to handle null values:

    int? nullableInt = GetNullableValue();
    int result = nullableInt ?? 0;
    
  13. C# pattern matching with nullable types

    Use pattern matching to check for null:

    int? nullableInt = GetNullableValue();
    if (nullableInt is null)
    {
        // Handle null case
    }
    else
    {
        // Process non-null value
    }