By now, a initialization of volumes such as arrays with index × = y
Take place. C# 13.0 also has an array Arbhikaran with an index from the end. (^x) = y
Possible with index operator ^
This has been since C# 8.0.
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.
However, the new syntax is possible only with the start of the object, not with other allocation.
The following code example shows the object volume with the exposure from the front from the front ×
And from the end (^x)
,
namespace NET9_Console.CS13;
internal class CS13_Indexer
{
class ZahlenGenerator
{
public string() Ziffern = new string(10);
}
///
/// C# 13.0: Objekt-Initialisierung mit Index vom Ende (^x) ("Implicit Indexer Access in Object Initializers")
///
public void Run()
{
CUI.Demo(nameof(CS13_Indexer));
CUI.H2("Array-Initialisierung mit Indexer von vorne nach hinten");
var dAlt = new ZahlenGenerator()
{
Ziffern = {
(0) = "null",
(1) = "eins",
(2) = "zwei",
(3) = "drei",
(4) = "vier",
(5) = "fünf",
(6) = "sechs",
(7) = "sieben",
(8) = "acht",
(9) = "neun",
}
};
foreach (var z in dAlt.Ziffern)
{
Console.WriteLine(z);
}
CUI.H2("NEU: Array-Initialisierung mit Indexer von hinten nach vorne");
var dNeu = new ZahlenGenerator()
{
Ziffern = {
(^1) = "neun",
(^2) = "acht",
(^3) = "sieben",
(^4) = "sechs",
(^5) = "fünf",
(^6) = "vier",
(^7) = "drei",
(^8) = "zwei",
(^9) = "eins",
(^10) = "null"
}
};
foreach (var z in dNeu.Ziffern)
{
Console.WriteLine(z);
}
CUI.H2("NEU: Array-Initialisierung mit Indexer in beide Richtungen");
var dNeu2 = new ZahlenGenerator()
{
Ziffern = {
(^1) = "neun",
(^2) = "acht",
(^3) = "sieben",
(^4) = "sechs",
(^5) = "fünf",
(4) = "vier",
(3) = "drei",
(2) = "zwei",
(1) = "eins",
(0) = "null"
}
};
foreach (var z in dNeu2.Ziffern)
{
Console.WriteLine(z);
}
CUI.H2("NEU: Array-Befüllung zu einem späteren Zeitpunkt");
// erstelle ein Array von int mit 10 Elementen
int() array1 = new int(10) { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// das geht nicht, Syntax nur bei Objektinitialisierung im Rahmen der Instanziierung erlaubt
//array1 = {
// (^1) = 9,
// (^2) = 8,
// (^3) = 7,
// (^4) = 6,
// (^5) = 5,
// (0) = 0,
// (1) = 1,
// (2) = 2,
// (3) = 3,
// (4) = 4
// }
// hier geht nur das:
array1(^1) = 9;
array1(^2) = 8;
array1(^3) = 7;
array1(^4) = 6;
array1(^5) = 5;
array1(0) = 0;
array1(1) = 1;
array1(2) = 2;
array1(3) = 3;
array1(4) = 4;
foreach (var item in array1)
{
CUI.LI(item);
}
}
}
(RME)