The 6 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…

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);

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; }
};



? 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;

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 { }
  } 
}

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);



✨ 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" })

Instead of:

if (e is MethodCallExpression { Method: { Name: "MethodName" } })`

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 }

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

{ Prop1: { Prop2: pattern } }

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;



? 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;

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


Print Share Comment Cite Upload Translate
APA
Dotnetsafer | Sciencx (2024-03-28T15:33:59+00:00) » The 6 Features of C# 10 you need to know NOW?. Retrieved from https://www.scien.cx/2021/09/27/the-6-features-of-c-10-you-need-to-know-now%f0%9f%a4%af/.
MLA
" » The 6 Features of C# 10 you need to know NOW?." Dotnetsafer | Sciencx - Monday September 27, 2021, https://www.scien.cx/2021/09/27/the-6-features-of-c-10-you-need-to-know-now%f0%9f%a4%af/
HARVARD
Dotnetsafer | Sciencx Monday September 27, 2021 » The 6 Features of C# 10 you need to know NOW?., viewed 2024-03-28T15:33:59+00:00,<https://www.scien.cx/2021/09/27/the-6-features-of-c-10-you-need-to-know-now%f0%9f%a4%af/>
VANCOUVER
Dotnetsafer | Sciencx - » The 6 Features of C# 10 you need to know NOW?. [Internet]. [Accessed 2024-03-28T15:33:59+00:00]. Available from: https://www.scien.cx/2021/09/27/the-6-features-of-c-10-you-need-to-know-now%f0%9f%a4%af/
CHICAGO
" » The 6 Features of C# 10 you need to know NOW?." Dotnetsafer | Sciencx - Accessed 2024-03-28T15:33:59+00:00. https://www.scien.cx/2021/09/27/the-6-features-of-c-10-you-need-to-know-now%f0%9f%a4%af/
IEEE
" » The 6 Features of C# 10 you need to know NOW?." Dotnetsafer | Sciencx [Online]. Available: https://www.scien.cx/2021/09/27/the-6-features-of-c-10-you-need-to-know-now%f0%9f%a4%af/. [Accessed: 2024-03-28T15:33:59+00:00]
rf:citation
» The 6 Features of C# 10 you need to know NOW? | Dotnetsafer | Sciencx | https://www.scien.cx/2021/09/27/the-6-features-of-c-10-you-need-to-know-now%f0%9f%a4%af/ | 2024-03-28T15:33:59+00:00
https://github.com/addpipe/simple-recorderjs-demo