Replacing if/else Statements in .NET Core

Vineet Sharma
2 min readJan 21, 2025

if/else statements are a common construct in programming, but they can sometimes lead to code that is verbose, hard to read, or difficult to maintain. This blog post will explore several techniques to replace if/else statements in .NET Core with more concise and modern approaches, improving code clarity and maintainability.

1. Use the Ternary Operator

For simple conditional assignments, the ternary operator offers a concise alternative to if/else.

Example:

Before:

int value;
if (condition)
{
value = 10;
}
else
{
value = 20;
}

After:

int value = condition ? 10 : 20;

2. Switch Expressions for Multiple Conditions

Starting with C# 8.0, switch expressions provide a clean and concise way to handle multiple conditions.

Example:

Before:

string result;
if (status == 1)
{
result = "Active";
}
else if (status == 2)
{
result = "Inactive";
}
else
{
result = "Unknown";
}

After:

string result = status switch
{
1 => "Active",
2 => "Inactive",
_ => "Unknown"
};

3. Dictionary Lookup for Value Mapping

When you need to map inputs to outputs, a dictionary can replace repetitive if/else blocks.

Example:

Before:

string role;
if (userType == "admin")
{
role = "Administrator";
}
else if (userType == "editor")
{
role = "Content Editor";
}
else
{
role = "Viewer";
}

After:

var roles = new Dictionary<string, string>
{
{ "admin", "Administrator" },
{ "editor", "Content Editor" }
};
string role = roles.ContainsKey(userType) ? roles[userType] : "Viewer";

4. Leverage Polymorphism for Complex Logic

For more complex decision-making based on types or roles, consider using polymorphism to avoid deeply nested if/else statements.

Example:

Before:

if (shape is Circle)
{
Console.WriteLine("This is a Circle");
}
else if (shape is Square)
{
Console.WriteLine("This is a Square");
}

After:

abstract class Shape
{
public abstract void Print();
}
class Circle : Shape
{
public override void Print() => Console.WriteLine("This is a Circle");
}
class Square : Shape
{
public override void Print() => Console.WriteLine("This is a Square");
}
// Usage:
shape.Print();

5. Functional LINQ Approaches

For operations on collections, LINQ can replace if/else logic with clean, functional expressions.

Example:

Before:

var evenNumbers = new List<int>();
foreach (var number in numbers)
{
if (number % 2 == 0)
{
evenNumbers.Add(number);
}
}

After:

var evenNumbers = numbers.Where(number => number % 2 == 0).ToList();

Conclusion

Replacing if/else statements in .NET Core with modern constructs like ternary operators, switch expressions, dictionaries, polymorphism, and LINQ can significantly enhance code readability and maintainability. While if/else has its place, exploring these alternatives can lead to cleaner and more efficient code.

Do you have a specific use case where you’re struggling with if/else? Share it in the comments, and we’ll explore a refactored solution together!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Responses (5)

Write a response

dict.ContainsKey(key) + dict[key] should be replaced with dict.TryGetValue(key, out var value)

10

Why overcomplicate things when the complications are not needed? A simple if else can do a lot of things as well.

6

Especially if (shape is Circle circle) { circle…..

1