DEV Community

Cover image for 5+1 Features of C# 10 you need to know NOW🤯
Dotnetsafer
Dotnetsafer

Posted on • Updated on

5+1 Features of C# 10 you need to know NOW🤯

With only two months left until the official release of C# 10 and Microsoft has opened a discussion about the new features and functions of the tenth version of its C# language. These enhancements are those announced in C# 10.0 Preview 7.

The features and improvements that Microsoft is going to implement in the next version of C# 10 are the following 👇


💥 Record types can seal ToString

Now in C# 10.0 version, you have the ability to add the sealed modifier when you override ToString in a record type.

Sealing the ToString method evade the compiler from synthesizing a ToString method for any derived record types. This function allows you to ensure all derived record types use the ToString method defined in a common base record type.

Microsoft advises us that this feature requires setting the element in the csproj file to preview.

What is the Record Keyword? 🤔

If you don't know what I'm talking about, this is normally used to define a reference type that provides built-in functionality for encapsulating data.

A simple example of what can be done with this is that you can create record types with immutable properties using standard property syntax 👇

public record Person(string FirstName, string LastName);
Enter fullscreen mode Exit fullscreen mode

And what better way to understand it than with a simple example from Microsoft 👇

public record Person
{
  public string FirstName { get; init; }
  public string LastName { get; init; }
};
Enter fullscreen mode Exit fullscreen mode

🏅 File-scoped namespace declaration

You can now use the new namespace declaration form to declare that all subsequently declared declarations are members of the declared namespace 👇

namespace NamespaceName;
Enter fullscreen mode Exit fullscreen mode

This new syntax, which will be implemented in the new version of C# 10, will save both vertical and horizontal space for the most common namespace declarations.

What is namespace keyword? 🤔

To clarify this, the namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.

namespace SampleNamespace
{
  class SampleClass { }
  interface ISampleInterface { }
  struct SampleStruct { }
  enum SampleEnum { a, b }
  delegate void SampleDelegate(int i);
  namespace Nested
  { 
    class SampleClass2 { }
  } 
}
Enter fullscreen mode Exit fullscreen mode

And sure, but…

What are file-scope namespace declarations? 🤔

These declarations in particular, allow you to declare all the types of a file, which are in a single namespace.

To go a little deeper, in this version of C# 10.0, the example is similar to the previous one that has been shown by Microsoft, but uses a file scope namespace declaration 👇

using System;
namespace SampleFileScopedNamespace;
class SampleClass { }
interface ISampleInterface { }
struct SampleStruct { }
enum SampleEnum { a, b }
delegate void SampleDelegate(int i);
Enter fullscreen mode Exit fullscreen mode

✨ Constant interpolated strings

To understand the Constant interpolated strings, first we need to understand String Interpolation.

The $ character identifies a string literal as an interpolated string. An interpolated string is a string literal that might contain interpolation expressions.

When an interpolated string is resolved to a result string, items with interpolation expressions are replaced by the string representations of the expression results.

Perfect, now my question is….

What Constant interpolated strings have in C# 10? 🤔

Talking about this new feature that C# version 10.0 will bring is const strings, which can be initialized using string interpolation only if the placeholders are themselves constant strings.

String interpolations can create more readable const strings as the const strings used in the application are constructed.
Placeholder expressions cannot be numeric constants because those constants are converted to strings at runtime. The culture currently in place, could affect the string representation.


🔥 Extended Property Patterns

First, I will explain what are the Extended property patterns. These patterns allow you to have property subpatterns refer to nested members, for example:

if (e is MethodCallExpression { Method.Name: "MethodName" })
Enter fullscreen mode Exit fullscreen mode

Instead of:

if (e is MethodCallExpression { Method: { Name: "MethodName" } })`
Enter fullscreen mode Exit fullscreen mode

Now that this has been clarified…

What Extended Property Patterns have in C# 10? 🤔

The next functionality that Microsoft discusses is that nested fields or properties could be referenced within a property pattern. The example Microsoft gives is a pattern of the form 👇

{ Prop1.Prop2: pattern }
Enter fullscreen mode Exit fullscreen mode

Microsoft tells us that it will be valid in C# 10.0 and later, and that this is equivalent to 👇

{ Prop1: { Prop2: pattern } }
Enter fullscreen mode Exit fullscreen mode

And this is valid in C# version 8.0 and all subsequent versions


⚡ Declaration and assignment in same deconstruction

This new change implemented by the new version, allows to remove the restriction of previous versions of C#. The example that Microsoft gives us in this case is 👇

…Previously, a deconstruction could assign all values to existing variables, or initialize newly declared variables:

// Initialization:
(int x, int y) = point;

// assignment:
int x1 = 0;
int y1 = 0;
(x1, y1) = point;
This restriction is eliminated in C# 10.0 👇
int x = 0; (x, int y) = point;
Enter fullscreen mode Exit fullscreen mode

🌐 Global using directives

You can now use the global modifier to any using directive. With this you can tell the compiler that the directive must be applied to all source files in the compilation.

Perfect, but as before…

What is the using directive? 🤔

This directive allows you to use types defined in a namespace without specifying the entire namespace of that type.

To summarize, the using directive imports all types from a single namespace, as shown in the following example 👇

using System.Text;
Enter fullscreen mode Exit fullscreen mode

You can apply two modifiers to a using directive:

  • The global modifier has the same effect as adding the same using directive to every source file in your project. This modifier was introduced in C# 10.0.

  • The static modifier imports the static members and nested types from a single type rather than importing all the types in a namespace.


If you liked this article, don't forget to FOLLOW US, so that you can be one of the first to read what's new in .NET.

And if you are reading this, it means that you belong to the 1% of the people who read the articles UNTIL THE END, tell me how many coffees ☕ you need per day to continue programming, if I see many coffees ☕ I will not feel alone and you will make my day!!! 😃👍

a

Discussion (8)

Collapse
zangassis profile image
Assis Zang

C#10 will make a lot of things easier, I hope development companies will lose some of the fear of upgrading their .NET versions. Thank you for the tips. 🎉

Collapse
fermantttnnn profile image
Ferman Quliyev

Almost nobody looked at the face of the struct since the aspnet web development became popular. I thought same fate are waiting for the record.

Collapse
hampzter profile image
Jonas Skoogh

Generally ☕☕☕ per day nowadays.

Collapse
ascot31 profile image
Alvaro Brena

🫖🫖🫖🫖🫖🫖🫖🫖🧉

Collapse
kennyf3 profile image
zaira69yt

At the moment today ☕ when normally it's like ☕☕☕☕☕

pd: As always very good article! Thanks for sharing this news with the community🤗

Collapse
hasindulanka profile image
Hasindu Lanka

☕☕☕ daily 😁

Collapse
srmagura profile image
Sam Magura

2 ☕ per day 😆

Looking forward to global usings and the new namespace syntax!

Collapse
minhhuyen93 profile image
Huyền Mingo

Look good and a lot of things we have at the moment.