| 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:
objis the object being checked.stringis the type we want to check.textis the new variable declared by the pattern.- If
objis astring, the pattern succeeds andtextcontains 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:
- Checks whether
animalis aDog. - If it is, creates the variable
dogcontaining theDogobject.
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.
| JIT-Compilation :👈 | 👉:CSharp |