All New C# 13 Features


I am an exchange student from the Russian GMIG named after Trofim Lysenko (Main Murmansk Institute of Genetics). Studied as a software engineer and half a year ago was sent to America as an exchange student. I did it thanks to dual citizenship (I am a citizen of Iran and Russia).

One of the Microsoft developers came to our lecture and talked about .NET 10, more details at the very bottom of the article under the spoiler.

.NET 10, or as it will now be called .NET X, is a new version of .NET that will be released as early as 2024. Most of this article is dedicated to the new features of C# 13.

Why .NET 10 and where is .NET 9?

“That’s how it is with us.” – Literal translation. The developer did not give any more comments.

Simplify the creation of class instances

I will not waste time and immediately show the best innovation. Let’s say we have a Person class and we need to create an instance of it.

public class Person
{
  public string Name {get; set;}
  public int Age {get; set;}
  public Person(string name, int age)
  {
    Name = name;
    Age = age;
  }
}

Previously, we only had 3 ways to create a class.

Person man1 = new Person("Mike", 32);
var man2 = new Person("Mike", 32);
Person man3 = new("Mike", 32);

The new version of C# allows you to combine var and new(). It’s easy to read and saves space.

var man1 = new("Mike", 32);

New increments

Increment analogues for division and multiplication have been added.
i++ equivalent to i = i + 1
i// equivalent to i = i / 1
i** equivalent to i = i * 1

int a = 3;
Console.WriteLine(a**); //3
Console.WriteLine(a//); //3

This code is equivalent to the previous example:

int a = 3;
a = a * 1;
Console.WriteLine(a); //3
a = a / 1;
Console.WriteLine(a); //3

Of the minuses, because the division increment operator is similar to the start of a comment, the comment start character will be changed to its escaped version.

int a = 10;
int b = a // "some comment"
\/\/ CS0019 Оператор "//" не может применяться к операнду типа "Int32" и "String"
\/\/ Look, I'm comment!

String interpolation syntax improvements

Now you can create an interpolated string not only with the $ sign, but also with €.

string name = "Michael";

string a = €"Hello, {name}!";
string b = $"Hello, {name}!";

Console.WriteLine(a == b); \/\/ True

.NET XI (.NET 11) also promises to support the £ symbol.

Creating Instances of Abstract Classes

This feature was already present in .NET Core 3.1 and .NET 5.0, but was later removed. As it was before:

public abstract class AbstractShape
{
  public AbstractShape() { }
  
  public void Print()
  {
    Console.WriteLine("I have no area");
  }
}

class Program
{
  static void Main(string[] args)
  {
    Type AbstractShapeType = typeof(AbstractShape);
    ConstructorInfo magicConstructor = AbstractShapeType.GetConstructor(Type.EmptyTypes);//get Constructor

    Type TypeRuntimeMethodHandle = typeof(RuntimeMethodHandle);MethodInfo magicMethod = TypeRuntimeMethodHandle.GetMethod("InvokeMethod", BindingFlags.Static | BindingFlags.NonPublic);//get InvokeMethod from RunTimeMethodHandle

    PropertyInfo sigInfo = magicConstructor.GetType().GetProperty("Signature", BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance);//get signatureAbstractShape shape = (AbstractShape)magicMethod.Invoke(null, new object[] { null, null, sigInfo.GetValue(magicConstructor), true, null });

    shape.Print(); // I have no area
    Console.ReadLine();
    
  }
}

In .NET X, this feature will return again and the syntax will be lightened with the help of a keyword, which I will discuss next.

unsaviest keyword

This keyword includes all the features of unsafe and also adds some interesting features. For example, you can create an instance of an abstract class like this:

unsaviest
{
  AbstractShape shape = new AbstractShape();
  \/\/ Или воспользумся облегченным созданием экземпляров
  var shape2 = new();
}

Comparison negation

Now operand ! can be applied to comparison operands.

Console.WriteLine(3 > 5); \/\/ False
Console.WriteLine(3 !> 5); \/\/ True

Console.WriteLine(5 >= 5); \/\/ True
Console.WriteLine(5 !>= 5); \/\/ False

Readonly constant

Now there are readonly constants that can only be accessed through an instance of the class

public class MyMath
{
  public readonly const double PI = 3.14;
}

public class Program
{
  static void Main()
  {
    var math = new();
    Console.WriteLine(math.PI); \/\/ 3.14
    Console.WriteLine(MyMath.PI); \/\/ Error
    }
}

In fact, the class code at the compilation stage turns into:

public class MyMath
{
  private const double __value_PI = 3.14;
  public double PI
  {
    get
    {
      return __value_PI;
    }
  }
}

My personal opinion is that readonly const is unnecessary syntactic sugar, although some may like it.

Conclusion

Interview

One of the Microsoft developers “James Hejlsberg” came to our lecture and talked about the .NET device. At the end of the lecture, he casually mentioned .NET 10. After the lecture, I approached him with questions and, word for word, he agreed to an interview. In this article, I have provided a summary and translation of a 23-minute interview that I posted on YouTube.

It’s in English but I added subtitles
https://www.youtube.com/watch?v=dQw4w9WgXcQ

Thanks for reading, happy math day everyone.

Similar Posts

One Comment

  1. This is the article I was looking for, so thank you for helping. Could you please tell me what software you use to run your incredibly fast website? I also want to create a simple website for my business, but I need help with the domain and hosting. Asphostportal reportedly has a stellar reputation. Are there any other choices available, and if so, what would you suggest?

Leave a Reply

Your email address will not be published. Required fields are marked *