Discover the Latest Preview Features in C# 13
Welcome back to our newsletter!
This weeks edition brings you the latest preview features in C#13 with a full rundown on how you can benefit from these new enhancements.
- Be sure to give them a spin after finishing this article.
Lets dive into the new features.
params collections
In C# 13, the params keyword has been enhanced to support any type that can be constructed via a collection expression, adding greater flexibility to method parameters. Traditionally, params worked with arrays, allowing method calls with a comma-delimited list or an array, like this:
public void WriteNames(params string[] names)
=> Console.WriteLine(String.Join(", ", names));
WriteNames("Mads", "Dustin", "Kathleen");
WriteNames(new string[] {"Mads", "Dustin", "Kathleen"});
Now, C# 13 extends this functionality to types like List<string>:
public void WriteNames(params List<string> names)
=> Console.WriteLine(String.Join(", ", names));
Furthermore, params can be used with IEnumerable<T>, allowing calls with a comma-delimited list or LINQ expressions:
public void WriteNames(params IEnumerable<string> names)
=> Console.WriteLine(String.Join(", ", names));
var persons = new List<Person>
{
new Person("Mads", "Torgersen"),
new Person("Dustin", "Campbell"),
new Person("Kathleen", "Dollard")
};
WriteNames("Mads", "Dustin", "Kathleen");
WriteNames(persons.Select(person => person.FirstName));
WriteNames(from p in persons select p.FirstName);
This makes method calls more flexible and coding more efficient.
overload resolution
In C# 13, overload resolution for params allows multiple overloads, enhancing flexibility and performance. Authors can create overloads for various types, such as IEnumerable<T> for LINQ support and ReadOnlySpan<T> or Span<T> to reduce allocations and improve efficiency. For example:
public void WriteNames(params string[] names)
=> Console.WriteLine(String.Join(", ", names));
public void WriteNames(params ReadOnlySpan<string> names)
=> Console.WriteLine(String.Join(", ", names));
public void WriteNames(params IEnumerable<string> names)
=> Console.WriteLine(String.Join(", ", names));
When a specific type is passed, the corresponding overload is used. For comma-delimited values or no values, the most suitable overload is selected. Using these overloads:
// IEnumerable overload is used
WriteNames(persons.Select(person => person.FirstName));
// array overload is used
WriteNames(new string[] {"Mads", "Dustin", "Kathleen"});
// most efficient overload is used: currently ReadOnlySpan
WriteNames("Mads", "Dustin", "Kathleen");
Multiple overloads provide convenience and performance benefits. Library authors should ensure all overloads have consistent semantics so callers don’t need to worry about which overload is used.
lock object
In .NET 9, a new System.Threading. Lock type has been introduced to improve mutual exclusion, offering potentially greater efficiency compared to locking on a standard System.Object instance. The System.Threading.Lock type is designed to become the primary locking mechanism in C# over time.
Recommended by LinkedIn
C# 13 simplifies the adoption of this new type. When the compiler detects that a lock statement is targeting a System.Threading.Lock object, it automatically generates calls to the System.Threading.Lock API and provides warnings if a Lock instance is mistakenly used as a regular object.
To take advantage of these improvements, developers need to update their project to target .NET 9 and change their lock statements from using object to System.Threading.Lock. For example:
// Old approach with object
private readonly object _lockObject = new object();
public void Method()
{
lock (_lockObject)
{
// Critical section
}
}
// New approach with System.Threading.Lock
private readonly System.Threading.Lock _lockObject = new System.Threading.Lock();
public void Method()
{
lock (_lockObject)
{
// Critical section
}
}
This update allows existing code to benefit from new runtime features with minimal changes.
🔥 Hot .NET Jobs by develop 🔥
now time for some fun!
Have you ever wondered how your zodiac sign might influence your coding style?
In this fun exploration, we match each zodiac sign with a programming language that embodies its key characteristics.
Let's dive into the astrological coding cosmos and discover which programming language you are!
Are you following us?