Declaring a constant double [] in C #?

I have several constants that I use, and my plan was to put them in a const array of double digits, however, the compiler did not allow me.

I tried declaring it like this:

const double[] arr = {1, 2, 3, 4, 5, 6, 73, 8, 9 }; 

Then I decided to declare it static readonly:

 static readonly double[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9}; 

However, the question remains. Why can't the compiler declare an array of const values? Or is it, and I just don't know how?

+45
c # static const readonly
Jul 10 '09 at 14:18
source share
6 answers

From MSDN ( http://msdn.microsoft.com/en-us/library/ms228606.aspx )

A constant expression is an expression that may be compilation time. Because the only way to create a nonzero value, the reference type [array] is intended to use the new operator, and because the new operator is not allowed in a constant expression, the only possible value for the constants of the reference types except the string is null.

+38
Jul 10 '09 at 14:26
source share

Perhaps this is due to the fact that

 static const double[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9}; 

actually the same thing to say

 static const double[] arr = new double[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9}; 

The value assigned to the constant must be ... const. Each reference type is not constant, and an array is a reference type.

As a result of my research, static readonly was used. Or, in your case with a fixed number of doublings, specify all individual identifiers.




Edit (2): A small sidenode, each type can be used by const, but the value assigned to it must be const. For reference types, the only thing you can assign is null:

 static const double[] arr = null; 

But it is completely useless. Strings are an exception, it is also the only reference type that can be used for attribute arguments.

+44
Jul 10 '09 at 14:25
source share

There is no way in C # to have a const array. You need to use indexes, properties, etc., so that the contents of the array do not change. You may need to overestimate the public side of your class.

Just point though ... Static readonly -IS NOT CONST -

This is perfectly true, not what you wanted:

 class TestClass { public static readonly string[] q = { "q", "w", "e" }; } class Program { static void Main( string[] args ) { TestClass.q[ 0 ] = "I am not const"; Console.WriteLine( TestClass.q[ 0 ] ); } } 

You will need to find other ways to protect your array.

+9
Apr 22 '10 at 18:42
source share

I donโ€™t know why you had to make it both permanent and readable. If you really want the whole array to be immutable, then the simple constant / readonly keyword will not help you, and even worse, it may also divert you to the wrong path.

For any immutable reference types, making them read-only means you can never override a variable but the content is still volatile. Example below:

 readonly double[] a = new double[]{1, 2, 3}; ... a = new double[] {2,3}; // this won't compile; a[1] = 4; // this will compile, run and result the array to {1, 4, 3} 

Depending on your context, there may be some solutions, one of them, if you really need a list of two, List a = new List () {1,2,3,4,5} .AsReadOnly (); will provide you with a two-word list.

+3
Apr 22 '10 at 20:33
source share

The problem is that you are declaring a constant double array, not an array of constant doubles. I don't think there is a way to have an array of constants due to the way arrays work in C #.

+2
Jul 10 '09 at 14:26
source share

a compiler error tells you why you cannot do this:

'arr' is of type 'double []'.
A const field of a reference type other than a string can only be initialized to zero.

+1
Jul 10 '09 at 14:25
source share



All Articles