Avatar ·

Hidden Features of C# You Might Not Know: Useful Tricks and Syntactic Sugar

Hello everyone! I'm a beginner C# developer, writing in .NET for about six months. Recently I came across a discussion on Stack Overflow about "hidden features" of C#. I got curious about what little-known but useful language features exist that are usually not shown in tutorials for beginners.\n\nFor example, I know about null-conditional operators (?.) and expression-bodied members (=>), but I feel like this is just the tip of the iceberg. Here's an example of my code where I want to simplify null checking and value assignment:\n\n```csharp\npublic string GetUserName(User user)\n{\n if (user != null && user.Name != null)\n {\n return user.Name;\n }\n return "Guest";\n}\n```\n\nI've heard that you can use the ?? and ?. operators, but I'm not sure how to combine them effectively. I've also seen strange things like