Reputation: 34175
what I want to do is really basic, I think. To code example shows it clearly.
class MyClass{
public string[] Bar;
}
MyClass Foo = new MyClass();
Foo.Bar = { "word", "word", "word" };
This code gives me an error in Visual Studio C#. (Only assignment, call, increment, decrement, and new object expressions can be used as a statement)
Is there a better way to provide an array to the class? The array could be const
for my part.
How can I provide an (const) array to a class from outside?
I don't want to use the constructor, because the array should be optionally.
Upvotes: 1
Views: 7900
Reputation: 22555
You should create array, Also you can do this initialization in Foo creation:
MyClass Foo = new MyClass{Bar = new []{ "word", "word", "word" }};
If you want initialize your array only once define it as readonly, and do initialization in constructor:
class MyClass{
public readonly string[] Bar;
public MyClass(string[] bar)
{
Bar = bar;
}
}
MyClass Foo = new MyClass (new[] { "word", "word", "word" } );
Edit: If you don't like initialize it in your constructor, you can use semi Singleton like pattern:
class MyClass
{
private static string[] bar;
public static string[] Bar
{
get { return bar; }
set
{
if (bar == null)
bar = value;
}
}
}
But if you add lock
statement it will be thread safe but may be performance decreases.
Upvotes: 1
Reputation: 39013
You can't make a const array and initialize it anywhere other than its declaration. You can make a readonly
array which can be initialized in the constructor. If you want to set the array's value somewhere else, it needs to be an ordinary data field.
Upvotes: 0
Reputation: 1499800
You can only initialize arrays using the {}
without the new
operator as part of a declaration (which in turn has to explicitly specify the array type). This has nothing to do with whether it's in the same class or not:
int[] x = { 1, 2, 3 }; // Fine
x = { 4, 5, 6 }; // Fail
x = new[] { 7, 8, 9 }; // Implicitly typed array as of C# 3
x = new int[] { 10, 11, 12 }; // Works with all versions of C#
See sections 12.6 (array initializers), 10.5 (field declarations), 8.5.1 (local variable declarations) and 7.6.10.4 (array creation expressions) of the C# 4 specification for details.
To answer your comment on Darin's post: no, there's no such thing as a "const" array in any sense I can imagine you mean. Even if you make the array variable readonly, like this:
private static readonly int[] Values = { 1, 2, 3 };
that only makes the variable read-only. Values
will always refer to the same array object (which will therefore always have 3 elements) but arrays themselves are always mutable. If you want to build a read-only collection, I'd suggest using ReadOnlyCollection<T>
, probably via List.AsReadOnly():
private static readonly ReadOnlyCollection<int> Values =
new List<int> { 1, 2, 3 }.AsReadOnly();
Upvotes: 5
Reputation: 16981
add new[] before brackets:
Foo.Bar = new[] { "word", "word", "word" };
Upvotes: 0
Reputation: 1038710
The correct array initializer syntax is:
Foo.Bar = new[] { "word", "word", "word" };
You can use { "word", "word", "word" }
only if you explicitly have specified the type on the left hand-side:
string[] foo = { "word", "word", "word" };
Upvotes: 4