JIT-Compilation :👈 👉:CSharp

Declaration Patterns in C#

What is Declaration Pattern in C#?

The Declaration Pattern is a pattern matching feature in C# used to check whether an object is of a particular type and, if it is, declare a variable of that type at the same time.

It is especially useful when working with objects whose actual runtime type may vary.

Basic Syntax


if (obj is string text)
{
    Console.WriteLine(text.Length);
}

Here:

  • obj is the object being checked.
  • string is the type we want to check.
  • text is the new variable declared by the pattern.
  • If obj is a string, the pattern succeeds and text contains the string value.

Example 1: Simple Declaration Pattern


object value = "Hello World";

if (value is string text)
{
    Console.WriteLine($"String value: {text}");
    Console.WriteLine($"Length: {text.Length}");
}

Output:

String value: Hello World
Length: 11

Instead of doing this:

 
if (value is string)
{
    string text = (string)value;
    Console.WriteLine(text.Length);
}

you can combine the type check and variable declaration:


if (value is string text)
{
    Console.WriteLine(text.Length);
}

Example 2: Declaration Pattern with Classes

Consider:


class Animal
{
    public string Name { get; set; }
}

class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Dog is barking");
    }
}

class Cat : Animal
{
    public void Meow()
    {
        Console.WriteLine("Cat is meowing");
    }
}
 

Now:


Animal animal = new Dog
{
    Name = "Bruno"
};

if (animal is Dog dog)
{
    Console.WriteLine(dog.Name);
    dog.Bark();
}

Here:

animal is Dog dog

does two things:

  1. Checks whether animal is a Dog.
  2. If it is, creates the variable dog containing the Dog object.

So you can immediately use:


dog.Bark();

Example 3: Declaration Pattern with else if

Declaration patterns are very useful when an object can have different types.


object value = 100;

if (value is string text)
{
    Console.WriteLine($"String: {text}");
}
else if (value is int number)
{
    Console.WriteLine($"Integer: {number}");
}
else if (value is double decimalNumber)
{
    Console.WriteLine($"Double: {decimalNumber}");
}

Output:

Integer: 100

The compiler checks each pattern until one matches.


Example 4: Declaration Pattern with switch

Declaration patterns become particularly powerful with a switch expression:

     
object value = 25;

string result = value switch
{
    string text => $"String: {text}",
    int number => $"Integer: {number}",
    double number => $"Double: {number}",
    _ => "Unknown type"
};

Console.WriteLine(result);

Output:

Integer: 25

Here:

int number

is a declaration pattern.


Declaration Pattern vs Traditional Casting

Traditional approach


if (animal is Dog)
{
    Dog dog = (Dog)animal;
    dog.Bark();
}

Declaration Pattern


if (animal is Dog dog)
{
    dog.Bark();
}

The declaration pattern is cleaner because the type checking and variable declaration happen together.


Important Point

The variable declared by the pattern is available only where the compiler knows that the pattern has successfully matched.

For example:


if (animal is Dog dog)
{
    dog.Bark();
}

dog is definitely a Dog inside the block.

💡 In simple terms

Declaration Pattern = Type check + variable declaration in one expression.

object
  ↓
Is it a Dog?
  ↓
Yes
  ↓
Declare dog
  ↓
Use dog.Bark()

This is one of the most useful C# pattern matching features, especially when working with inheritance, object, polymorphism, and switch expressions.

Back to Index
JIT-Compilation :👈 👉:CSharp