79254895

Date: 2024-12-05 13:41:49
Score: 1
Natty:
Report link

There is actually a difference for empty arrays.

As was mentioned in this SO answer https://stackoverflow.com/a/79254772/2893207 by @LMA the empty array in case of curly braces will create a new array instance:

int[] array1 = { };
int[] array2 = { };
Console.WriteLine(ReferenceEquals(array1, array2)); // Prints false

Collection expression for the empty array is optimized, it uses the Array.Empty<>() cached object.

int[] array1 = [];
int[] array2 = [];
int[] array3 = Array.Empty<int>();
Console.WriteLine(ReferenceEquals(array1, array2)); // Prints true
Console.WriteLine(ReferenceEquals(array1, array3)); // Prints true
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @LMA
Posted by: SENya