New Probability for REF structure in .NET 9.0 (11): C# 13.0

0
3
New Probability for REF structure in .NET 9.0 (11): C# 13.0


Since C# 7.2 are structures that are always on the stack and never increase on the pile: ref structC# 13.0 has the use of Microsoft ref struct extended.

Advertisement





Dr. Holder Schwichtenberg Technical Manager of the Network of experts www.it-visions.de, which supports many medium sizes and large companies through advice and training with 53 renowned experts as well as software development. Through his appearance in many national and international expert conferences as well as his appearance in more than 90 expert books and more than 1,500 expert articles, Holgar Schwichtanberg is one of the most famous experts for .NET and web techniques in Germany.

Javaland 2025: Safe tickets for Community Conference nowJavaland 2025: Safe tickets for Community Conference now

Now this kind can happen:

  • Apply the interface. However, the restriction applies that the structure cannot be converted into an interface type, as the compiler will have to make boxing internal.
  • Use as a typing. However, you have to do generic type or generic method where T : allows ref struct Use.
  • In iterers with yield used. However, the structure should not last longer than the current runs of repetition.
  • Incredible methods Task Or Task Distribute, be used.

Also, however, if you have a type ref struct Declared, boxing is no longer possible. Using the ref struct Hence it is limited. For example, you can’t array any other List Mentioned from it.

The following code shows its type ref structThis applies an interface:

internal interface IPerson
{
  int ID { get; set; }
  int Name { get; set; }
}

// NEU seit C# 13.0: ref struct kann Schnittstelle implementieren
ref struct Person : IPerson 
 {
  public int ID { get; set; }
  public int Name { get; set; }
  // ToString()
  public override string ToString()
  {
   return "Person #" + ID + " " + Name;
  }
 }
}
 
class Client
{
 public void Run()
 {
  Person p = new Person();
  p.ID = 1;
  p.Name = 2;
  Console.WriteLine(p.ID);
  Console.WriteLine(p.Name);
 
  // Das ist alles nicht erlaubt!
  // IPerson i = p; // Casting auf Schnittstelle
  // List PersonList = new(); // List
  // PersonList() PersonArray = new Person(10); // Array
 }
}


(RME)

IX Workshop for Developers and Architects: IT Management with TerraformIX Workshop for Developers and Architects: IT Management with Terraform

LEAVE A REPLY

Please enter your comment!
Please enter your name here