Autor Beitrag
OldCat
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 77

Win 10 Pro x64
C# (VS 2019), (VS 2022)
BeitragVerfasst: Sa 08.01.22 18:03 
Hey liebe Gemeinschaft :beer:

Heute bin ich über Code gestolpert, der mich einmal mehr die Achseln zucken lässt:

ausblenden C#-Quelltext
1:
2:
3:
4:
var phoneDirectory = new Dictionary<string, (string phone, string mobile, string email)>
{

};


Ist das eine zusammengesetzte Datenstruktur aus einem Dictionary und einem named Tupel? Oder einfach nur eine mögliche Überladung von Dictionary? Konnte in den Microsoft.docs, Überladungen für das Dictionary keine passende Überladung finden :gruebel:
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Sa 08.01.22 19:35 
Ja, durch die Klammern wird ein Tupel definiert, welches dann den Wert des Dictionary-Eintrags angibt (und mit einem string als Schlüssel).
Ein neuer Eintrag kann also z.B. so hinzugefügt werden:
ausblenden C#-Quelltext
1:
phoneDirectory.Add("meine Daten", ("0123 456789""0152 3456789""name@mail.de"));					

Für diesen Beitrag haben gedankt: OldCat
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Sa 08.01.22 19:41 
docs.microsoft.com/d...n-types/value-tuples

Sollte man aber mit Vorsicht genießen, da es die Lesbarkeit des Codes deutlich negativ beeinflussen kann.
In kleinen Rahmen nutze ich das aber ganz gerne, nur wenn es public wird, sollte es dafür schon einen guten Grund geben.

Für diesen Beitrag haben gedankt: OldCat
OldCat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 77

Win 10 Pro x64
C# (VS 2019), (VS 2022)
BeitragVerfasst: So 09.01.22 10:20 
Vielen lieben Dank an euch, das hat mir die Augen geöffnet :)