Few days ago I saw a thread on Twitter where developers shared their favorite C# features. It was very interesting to follow the discussions there since I never really thought about what are really my favorite C# features. It was food for thought for me so the past days I tried to observe myself while coding with the clear goal in mind to understand what my favorite C# features really are. I managed to create a shortlist and thought it might be useful to share it. Maybe somebody will find something useful in here.
Before I start listing and talking about my favorite C# features please note that the order I mention them is really random.
1. String interpolation
In my opinion this is one of the most underrate features of C#. Partly because most of the tutorials you find online and books you might read usually use the composite formatting of strings. I remember very well when I was starting to learn programming on my own I found the composite formatting of string very unfriendly, difficult to follow and easy to get it wrong. Let’s look at some code:
1 2 3 4 | string name = "Dan"; int age = 35; Console.WriteLine("My name is {0} and I am {1} years old!", name, age); Console.WriteLine($"My name is {name} and I am {age} years old!"); |
The first Console.WriteLine uses composite formatting to represent the desired string, while the second one uses string interpolation. For me, the first one is unfriendly and difficult to write especially when you need to integrate several variables in the string. The second one follows a more natural writing flow and even when reading it it’s much easier to grasp since you don’t have to look several tabs right and guess what variable goes in which placeholder.
I’m always using string interpolation in my code and the courses I deliver for aspiring software developers. Clearly, I also mention and show the composite formatting, but during classes and exercises I then always use string interpolation.
2. Auto-implemented properties
This struck me literally while I was writing code and had a specific scenario where I needed to implement a getter an a setter. In the C# world we are so accustomed to use auto-implemented properties that we don’t even perceive this as a feature. However, as it seems not all programming language have this luxury. The good thing about auto-implemented properties is that it prevents you writing a lot of boilerplate code like this:
1 2 3 4 5 6 | private string _firstName; public string FirstName { get => _firstName; set => _firstName = value; } |
Just think about all the lines the code you need to write just to implement 15 very simple properties like the one above! Luckily in C# we can simply have only one line of code per property:
1 | public string LastName { get; set; } |
Sure, I’m aware that in the first sample you can write both the getter and the setter on one line of code, but it gets less readable. And you still need a field. Therefore, auto-implemented properties is a cool C# feature.
3. LINQ
Many developers at the beginning of their career don’t even realize how often they actually use LINQ. In my opinion this is mainly because there is a common misconception that you’re using LINQ only when using query syntax. Of course, this is not true since LINQ also supports a method syntax that is totally different in comparison to the query syntax. But it’s still LINQ. Therefore this code:
1 2 3 4 | var filtered = from s in students where s.FirstName == "Dan" orderby s.LastName select s; |
…is does actually perform the exact same thing as:
1 2 | var alsoFildered = students.Where(s =>; s.FirstName == "Dan") .OrderBy(s =>; s.LastName); |
It’s still LINQ, but the syntax is different.
Why is LINQ one of may favorite C# features? Well because we need to filter, order or group collections almost every day. With LINQ it’s intuitive, it’s fast and it has two different flavors. Are you coming from a SQL background? The LINQ query syntax will be your friend! Is SQL not your strong point? You’ll probably love the method syntax. Therefore, LINQ is easy to use and very powerful.
4. Extension methods
Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.
Nowadays a lot of the development we do relies on 3rd party libraries that we usually install as NuGet packages. These libraries comes with their specific types. In my experience these 3rd party types often need some functionality that bridge the gap between the library itself and the business logic in my application. In this scenario I find extension methods particularly useful and therefore a neat feature to have in C#.
For those that are not that familiar with extension methods here is a sample that extends the string class by adding my own logic for making all characters uppercase:
1 2 3 4 | public static string ToUppercase(this string myString) { return myString.ToUpper(); } |
When I have a string object and use the member access operator (“.”), I’ll have my ToUppercase() method appear in IntelliSense. This means that I have extended the string class with new functionality. Of course, this is just a demo example since the string class already has the ToUpper() method that does exactly this, but the purpose here was to show how a type can be extended by using an extension method.
5. Async and await
I know that this topic is a very complex one and I plan to cover it indeed in more details in a separate blog post aimed for a beginner audience at first. However, generally speaking the idea of asynchronous programming is in itself very complex and even philosophical I would dare to say. A big “Congrats” to the C# designers that manage to bring the concept of asynchronous programming to C# in a way that is fairly easy to understand and implement when you compare it to the complexity of the entire background. I also use async and await literally every day so this features earns its spot in this list.
So, these are my favorite C# features. What are yours?
How useful was this post?
Click on a star to rate it!
Average rating / 5. Vote count:
Dan Patrascu-Baba
Latest posts by Dan Patrascu-Baba (see all)
- Configuration and environments in ASP.NET Core - 25/11/2019
- GraphQL in the .NET ecosystem - 19/11/2019
- A common use case of delegating handlers in ASP.NET API - 12/11/2019