Question about array initializers, const 
Author Message
 Question about array initializers, const

Could someone tell me why the following is OK:
public String[] test = {"Three","Two","Eight","Nine"};

while the following:

public const String[] test = {"Three","Two","Eight","Nine"};

results in the error "CS0623: Array initializers can only be used in a
variable or field initializer. Try using a new expression instead."



Fri, 10 Jun 2005 16:54:40 GMT  
 Question about array initializers, const

Quote:
> Could someone tell me why the following is OK:
> public String[] test = {"Three","Two","Eight","Nine"};

> while the following:

> public const String[] test = {"Three","Two","Eight","Nine"};

> results in the error "CS0623: Array initializers can only be used in a
> variable or field initializer. Try using a new expression instead."

It is limitation of C# (maybe even Framework's). This is how it works.
const can be used only for simple types.
You can use readonly modifier instead (however it is not full alternative).
--
Nesterovsky Vladimir



Fri, 10 Jun 2005 17:11:24 GMT  
 Question about array initializers, const
That did the trick.  Thanks!


Quote:
> > Could someone tell me why the following is OK:
> > public String[] test = {"Three","Two","Eight","Nine"};

> > while the following:

> > public const String[] test = {"Three","Two","Eight","Nine"};

> > results in the error "CS0623: Array initializers can only be used in a
> > variable or field initializer. Try using a new expression instead."

> It is limitation of C# (maybe even Framework's). This is how it works.
> const can be used only for simple types.
> You can use readonly modifier instead (however it is not full
alternative).
> --
> Nesterovsky Vladimir




Fri, 10 Jun 2005 22:34:58 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. const variables with initializers

2. Static const initializers

3. Array of const pointers to const variable.

4. problem with array of const pointers to const data

5. A question on const arrays

6. A question on const arrays

7. question about const and array

8. Static Arrays, Initializers, Managed C++

9. Clean way to const-cast arrays of arrays?

10. Array initializers

11. Non constant array initializers

12. pointer to array of char and static initializer?

 

 
Powered by phpBB® Forum Software