Hidden Features of C#?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
C# is a modern, versatile programming language developed by Microsoft and used widely for building a wide spectrum of applications including but not limited to desktop applications, mobile apps, cloud-based services, and large-scale enterprise systems. Though many developers are familiar with its primary features, C# holds numerous hidden gems that can significantly improve coding efficiency, readability, or performance. Here, we explore some of these lesser-known features.
1. Caller Information Attributes
C# allows you to obtain information about the caller to a method, which can be quite handy for debugging or logging purposes without manually passing parameters about the call site.
CallerFilePathAttribute- path of the source file that contains the callerCallerLineNumberAttribute- line number in the source file at which the method is calledCallerMemberNameAttribute- method or property name of the caller
Example
When you call LogMessage("Hello, World!"); from any method, the output includes the caller information automatically filled by the compiler.
2. Pattern Matching Enhancements
Introduced gradually from C# 7 onwards, pattern matching allows for more expressive and concise code, especially when dealing with type checking or extracting data from types.
Example
This feature checks if an order object is of type Order, has a Status property equal to "Processed", and retrieves the customer's name if available.
3. Local Functions
Local functions, introduced in C# 7, enable you to define methods inside the scope of another method to encapsulate utility functions that are relevant only in that particular scope.
Example
This local function Fib is used to compute a Fibonacci number and is only accessible within the Fibonacci method.
4. Index and Range Operators
C# 8 introduced index ^ and range .. operators that simplify array or collection manipulations significantly.
Example
5. Expression-Bodied Members
Initially limited to methods and read-only properties, expression-bodied members were expanded in later versions to include constructors, finalizers, and more.
Example
Summary Table of Hidden Features
| Feature | Since Version | Description |
| Caller Information | C# 5 | Attributes to get information about the caller. |
| Pattern Matching | C# 7 | Enhanced type checks and deconstruction. |
| Local Functions | C# 7 | Methods within methods for encapsulated logic. |
| Index and Range | C# 8 | Simplified syntax for accessing data sequences. |
| Expression-Bodied Members | C# 6 | Syntax shortcut for defining members of a class. |
Conclusion
These hidden features of C# not only provide syntactic sugar but also introduce more efficient ways to write and maintain code. They collectively contribute towards making C# a powerful, readable, and pleasant programming language for a vast array of applications. Whether you're building complex systems or simple utilities, understanding and leveraging these hidden features can significantly enhance your development capabilities in C#.
Related reading
- Hide Console Window in C Console Application
- High thread count stuck in GCFrame causes high CPU usage
- HintPath vs ReferencePath in Visual Studio
- Horrible performance using SqlCommand Async methods with large data
- Host.CreateDefaultBuilder vs Host.CreateApplicationBuilder in .NET Platform Extension 7
- How are .NET 4 GUIDs generated?
- How are strings passed in .NET?
- How async works in ASP.NET web applications?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.