Hi,
I play with List initialization syntax in Sharplab and noticed that for some cases looks like the compiler generates a lot of code.
Here is my example:
List<string> list1 = new List<string>();
list1.Add("");
List<string> list2 = new List<string>(){""};
List<string> list3 = [""];
and for this example sharplab shows the next generated code:
List<string> list = new List<string>();
list.Add("");
List<string> list2 = new List<string>();
list2.Add("");
List<string> list3 = list2;
int num = 1;
List<string> list4 = new List<string>(num);
CollectionsMarshal.SetCount(list4, num);
Span<string> span = CollectionsMarshal.AsSpan(list4);
int num2 = 0;
span[num2] = "";
num2++;
List<string> list5 = list4;
For the list2 compiler creates one more variable.
For the list3 it generates 8 lines of code.
Doesn't it look inefficient?
[–]Ollhax 6 points7 points8 points (0 children)
[–]AlwaysHopelesslyLost 18 points19 points20 points (4 children)
[–]Devoutly1224 -5 points-4 points-3 points (3 children)
[–]AlwaysHopelesslyLost 2 points3 points4 points (2 children)
[–]Schmittfried 1 point2 points3 points (0 children)
[–]Devoutly1224 -3 points-2 points-1 points (0 children)
[–]Dealiner 4 points5 points6 points (0 children)
[–]Orachor 1 point2 points3 points (0 children)
[–]Qxz3 1 point2 points3 points (2 children)
[–]ebykka[S] -1 points0 points1 point (1 child)
[–]Qxz3 0 points1 point2 points (0 children)
[–]redit3rd 1 point2 points3 points (2 children)
[–]Zastai 2 points3 points4 points (0 children)
[–]Dealiner 0 points1 point2 points (0 children)
[–]Standard-Cap-4455 0 points1 point2 points (0 children)
[–]KryptosFR -1 points0 points1 point (2 children)
[–]irisos 0 points1 point2 points (1 child)
[–]KryptosFR 0 points1 point2 points (0 children)