-1

Initializing an array is ok in these 3 cases

string[] To = { "one@g.com", "two@g.com" };
string[] To = new[] { "one@g.com", "two@g.com" };
string[] To = new string[] { "one@g.com", "two@g.com" };

But when using it as a parameter the first option is not valid so this is valid

MethodWithAnArrayParam(new string[] { "hi@t.com", "hi@t.com" });
MethodWithAnArrayParam(new [] { "hi@t.com", "hi@t.com" });

This gives an error

   MethodWithAnArrayParam({ "hi@t.com", "hi@t.com" });

Why?

Victor Hugo Terceros
  • 2,969
  • 3
  • 18
  • 31
  • 2
    What makes you think its invalid? What is the method signature for `.Send()`? – maccettura Mar 23 '18 at 20:27
  • The posted code does not help much – Héctor M. Mar 23 '18 at 20:29
  • Because `{ "one@g.com", "two@g.com" }` is not a valid C# array initializer. You need to tell C# which collection to build, array is not the default collection type – vc 74 Mar 23 '18 at 20:29
  • Possible duplicate of [Initializing a string array in a method call as a parameter in C#](https://stackoverflow.com/questions/1416500/initializing-a-string-array-in-a-method-call-as-a-parameter-in-c-sharp) – UnholySheep Mar 23 '18 at 20:29
  • Possible duplicate of [Can't use an “inline” array in c#?](https://stackoverflow.com/questions/30509177/cant-use-an-inline-array-in-c) – TVOHM Mar 23 '18 at 20:29
  • Because this is how the c# language defines an array literal. Just the curly brackets don't define an array literal. https://stackoverflow.com/questions/10921844/how-to-return-an-array-literal-in-c-sharp – oliver Mar 23 '18 at 20:31
  • @oliver That's not a literal. – Servy Mar 23 '18 at 20:38
  • @Servy: okay, maybe not by strict definition of the language standard... but in a more casual sense meaning a character sequence that translates to an initial value of a data structure – oliver Mar 23 '18 at 20:42
  • @oliver The term for that is a constructor, or potentially an initializer. A literal is a *value* of an object that is known at compile time. There are only a few types that have meaningful literals, just strings and numeric types (and also `null` and `default(...)`). – Servy Mar 23 '18 at 20:46
  • Why? Because that's how the language was designed... – Heretic Monkey Mar 23 '18 at 21:14

2 Answers2

2

Try using:

new[]{ "one@g.com", "two@g.com" }

Edit: Or you can use

new string[]{ "one@g.com", "two@g.com" }

See: Implicit Typed Arrays

LeviTheOne
  • 105
  • 9
2

Arrangement declarations are made using the keyword new

Try this

string TO = new string[]{ "one@g.com", "two@g.com" };

Also you can make use of generic arrays

var TO = new[]{ "one@g.com", "two@g.com" };

The data type of the same is given automatically automatic according to the value given:

new [] { "Hello "," World "} string array
new [] {0, 1, 2} int array
...

All this is invalid because at no time you tell the compiler that it is an Array, since {} has several different uses

emailProvider.Send("hello", { "one@g.com", "two@g.com" }, null, "subject");

emailProvider.Send("hello", (new string[] { "hi@t.com", "hi@t.com" }), null, "subject"); 

You can not create a statement without shadow and unassigned in the place of a parameter

bool Send(string message, string[] to, string[] bccTo, string subject);
maccettura
  • 10,514
  • 3
  • 28
  • 35
Héctor M.
  • 2,302
  • 4
  • 17
  • 35