# C# Hidden Gems: Underrated Features You Should Use
C# has grown into one of the most popular programming languages in the world, largely due to its versatility and powerful features. While developers often rely on well-known constructs, there are several underrated features in C# that can optimize your code, enhance readability, and improve performance. Let’s explore some of these hidden gems that you might be overlooking.
## 1. Expression-bodied Members
Introduced in C# 7.0, expression-bodied members allow for a more concise syntax when defining methods, properties, and constructors. Instead of using verbose syntax, you can use an arrow (`=>`) to create more readable code. For instance, rather than writing a full method body for trivial getters or single-expression methods, you can simplify them as follows:
“`csharp
public class Circle
{
public double Radius { get; }
public double Area => Math.PI * Radius * Radius;
public Circle(double radius) => Radius = radius;
}
“`
This leads to cleaner and more maintainable code, especially for simple properties.
## 2. Null-Conditional Operators
The null-conditional operator (`?.`) is a powerful addition to C# that allows for safer null checks without verbose if-statements. This operator enables developers to avoid NullReferenceExceptions and simplifies code that deals with potentially null objects:
“`csharp
string username = user?.Profile?.Username;
“`
In this example, if `user` or `Profile` is null, `username` will simply be null, rather than causing an exception. It makes accessing deeply nested properties more seamless and less error-prone.
## 3. Tuples
Tuples, introduced in C# 7.0, provide a lightweight way of grouping multiple values without needing to define a class or struct. This feature is particularly useful for methods that need to return multiple values:
“`csharp
public (int max, int min) GetMinMax(int[] numbers)
{
return (numbers.Max(), numbers.Min());
}
“`
Using tuples can save time and reduce boilerplate, making your methods cleaner and simpler to understand.
## 4. Pattern Matching
Pattern matching, which has progressively evolved through C# 7.0 and 8.0, allows developers to write more expressive code with switch statements, if checks, and data type checks. It enables concise checks and object manipulations, like so:
“`csharp
public string Describe(object obj) => obj switch
{
int i => $”Integer: {i}”,
string s => $”String: {s}”,
null => “Null object”,
_ => “Unknown type”
};
“`
This approach provides a robust way to handle various data types and enhances code clarity significantly.
## 5. Default Interface Methods
With C# 8.0, default interface methods were introduced, allowing developers to provide a default implementation for methods in an interface. This feature significantly enhances the evolution of interfaces without breaking existing implementations:
“`csharp
public interface ILogger
{
void Log(string message);
void LogError(string message) => Log($”Error: {message}”);
}
“`
By allowing methods to be defined in interfaces, developers can add new functionality while maintaining compatibility with existing implementations.
## Conclusion
C# is packed with features that extend beyond the basics. By leveraging these underrated gems—expression-bodied members, null-conditional operators, tuples, pattern matching, and default interface methods—you can write more efficient, cleaner, and more maintainable code. These features, often underutilized, can simplify your development process and enhance your overall programming experience. Embrace them in your next project, and watch your