2

I'm C/C++ to C# developer.

I need to do something like below,

#define LEN 20 int data[LEN]; 

Since C# doesn't support #define, many forums suggests to declare a const variable. A const variable might help me for below, but not for above.

for (int a = 0; a < LEN; a++) { x += data[LEN - a - 1]; } 

So, how do I use the constant for array declarations as well?

7
  • 10
    Even in C++ you shouldn't use preprocessor macros to define constants... Commented Mar 6, 2018 at 13:33
  • 8
    What do you mean by "not for above"? int x = data[LEN]; doesn't declare an array-type variable, nor does it create an array - that has nothing to do with constants, it's just incorrect syntax. You could use int[] data = new int[LEN];... Commented Mar 6, 2018 at 13:34
  • 1
    1) "how do I use the constant for array declarations as well?" The thing is.. The array size doesn't have to be constant in C#, for the same reason as you don't need to pass constant value to new[LEN] in C++. There are no arrays with automatic storage duration, as there is in C++. 2) #define doesn't create a constant. It is merely a textual replacement, which uses find-and-replace to replace every occurence of LEN to 20. Commented Mar 6, 2018 at 13:45
  • @UlrichEckhardt can you please share me why we shouldn't do so? Commented Mar 6, 2018 at 15:09
  • @JonSkeet "above" meaning declaring an array. The statement int x = data[LEN]; is corrected now. Commented Mar 6, 2018 at 15:12

4 Answers 4

1

C# lets you create constants of several built-in types using const keyword.

Constants can be numbers, Boolean values, strings, or a null reference.

Here is a small example:

public class Demo { public const int Length = 20; public void Main(string[] args) { var data = new int[Length]; for (var i = 0 ; i != Length ; i++) { data[i] = 2*i + 3; } } } 

Note: C# naming guidelines suggest avoiding all-caps naming.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for naming guidelines.
1

Actually you should use it for the instantiation of the array:

const int LEN = 20; int[] data = new int[LEN]; 

2 Comments

Doesn't answer the question. What is LEN?
Well I assumed that it was clear, since the issue was related to declaration syntax, I modified my answer to be more clear ^^
0

you could do something like this

static readonly int LEN = 20; static void Main(string[] args) { int[] data = new int[LEN]; for (int a = 0; a < LEN; a++) { int x = data[LEN]; } } 

please note that that date array will be default initialized to 0.

also, you will get System.IndexOutOfRangeException for int x = data[LEN]; because you are asking for the 20th index and there is no such index in the array

2 Comments

he said he's a c++ developer but still doesn't know that arrays are zero based indexing... strange
@sLowDowN Thank you nitpicker. Question is updated to satisfy you.
-1

You should not use constants to declare an array length. Also, there are many data structures in C# that allow you to have a dynamic size array/list/dictionary...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.