I am trying to compile following code in C#:
String[] words = {"Hello", "Worlds"};
words = {"Foo", "Bar"};
And I am getting compilation errors like:
Error 1 Invalid expression term '{'
Error 2 ; expected
Error 3 Invalid expression term ','
On the other hand if I try
String[] words = { "Hello", "Worlds" };
words = new String[] {"Foo", "Bar"};
It compiles fine. As per MSDN,
int[] a = {0, 2, 4, 6, 8};
it is simply a shorthand for an equivalent array creation expression:
int[] a = new int[] {0, 2, 4, 6, 8};
Why doesn't the first code sample compile?