Home DEVELOPER New in .NET 9.0 (8)

New in .NET 9.0 (8)

0


The first version of C# has since so -called variadic parameters (CF. Wikipedia entry for varied works)), With which a method can get a long list of a type of parameters if it can be obtained with keywords params Has been started.

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.

The following codes show the previous version of a wadic parameter with arrays:

public void MethodeMitBeliebigVielenParametern_Alt(string text, 
                                                 params int() args)
{
 CUI.H2(nameof(MethodeMitBeliebigVielenParametern_Alt));
 CUI.Print(text + ": " + args.Length);
 foreach (var item in args)
 {
  CUI.LI(item);
 }
}

For example, the method can be called as follows:

MethodeMitbeliebigVielenParametern_Alt("Anzahl Zahlen", 1, 2, 3);
MethodeMitbeliebigVielenParametern_Alt("Number of numbers",
                                       1, 2, 3, 4);

The new in C# 13.0 is that instead of an array in variedic parameters, generic types can also be used (hence parameter collection) params Span,

public void MethodeMitBeliebigVielenParametern_Neu(string text,
                                                   params Span args)
{
 CUI.H2(nameof(MethodeMitBeliebigVielenParametern_Neu));
 CUI.Print(text + ": " + args.Length);  // statt args.Length
 foreach (var item in args)
 {
  CUI.LI(item);
 }
}

Similarly, the call is then flexible with the parameter array:

MethodeMitBeliebigVielenParametern_Neu("Anzahl Zahlen", 1, 2, 3);
MethodeMitBeliebigVielenParametern_Neu("Number of numbers", 
                                       1, 2, 3, 4);

Then these are normal types of types params C# 13.0 allowed:

  • Collections.Generic.IEnumerable
  • System.Collections.Generic.IReadOnlyCollection
  • Collections.Generic.IReadOnlyList
  • System.Collections.Generic.ICollection
  • Collections.Generic.IList
  • All classes Collections.Generic.IEnumerableimplement
  • Span
  • ReadOnlySpan

If possible, you should have a parameter list Span Or ReadOnlySpan Give so much that the parameter handover is completely through the stack and no storage allocation is done on the pile.

The way you can hand over a prepared array in a parameter array, which is then distributed in parameters

MethodeMitBeliebigVielenParametern_Alt(
  "Anzahl Zahlen - übergeben als Int-Array", 
  (1, 2, 3));

Can you also hand over one of the above new types if the method is expected to be a parameter collection:

MethodeMitBeliebigVielenParametern_Neu(
  "Anzahl Zahlen - übergeben als List", 
  new List { 1, 2, 3 });

Or

MethodeMitBeliebigVielenParametern_Neu(
  "Anzahl Zahlen - übergeben als List", 
  (1, 2, 3));

Note: The following syntax does not work:

MethodeMitBeliebigVielenParametern_Neu(
  "Anzahl Zahlen - übergeben als List", 
  new List (1, 2, 3 ));

It’s not possible there new List (1, 2, 3 ) A three -related array of List created.


(RME)

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version