Set Elements of Array to Same Value in C#

  • Thread starter Thread starter Sojourner01
  • Start date Start date
  • Tags Tags
    Arrays Stupid
Click For Summary
Setting every element of an array to the same value in C# cannot be achieved in a single line of code directly. Users have pointed out that while shorthand array initialization is possible with specific values, such as char[] myChars = new char[]{'a', 'b', 'c'}, there is no built-in syntax for initializing all elements to a single value without iteration. The common method involves using a loop, such as a for loop, to assign the desired value to each index. Some users suggested creating a custom class, like BetterArray, which allows for initialization with a default value and length, but this still involves some form of iteration internally. The discussion highlights a perceived limitation in C# regarding array initialization, with some expressing frustration over the lack of a constant initialization expression for arrays, suggesting it may have been intentionally excluded to avoid potential errors. Ultimately, the consensus is that iteration remains the practical approach for this task in C#.
Sojourner01
Messages
371
Reaction score
0
For some reason, I can't work out how to set every element of an array to the same value in a single line, in C#. For example, I can't do this:

char[] array_of_chars = new char[limit];
array of chars[] = X

or:

char[] array_of_chars = new char[limit];
array of chars[0:limit] = X

So how is it done? Can it be done, or do I have to iterate? If so, that's a very stupid omission from the language.
 
Technology news on Phys.org
There is the following shorthand, but it's not exactly what you describe:
Code:
char[] myChars = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g'};
I think that's the best you can do in C# without iterating, i have not seen another way, perhaps you can hack something using Lambda expressions in .NET 3.0+, but probably nothing really worthwhile.

You can after all do:
Code:
char[] myChar = new char[100];
for(int i=0; i<myChar.length; i++){myChar[i] = 'X';}
That's one line, and you know any syntax in C# for initializing would be converted to the iteration form when compiling, so it's really non-essential, though admittedly it would be cleaner.
 
Last edited:
You can also create a class that wraps around an array or list that supports initialization, such as:
Code:
public class BetterArray<T> : List<T>{
    public BetterArray(T DefaultValue, int Length) : base(Length)
    {
        for (int i = 0; i < Length; i++)
        {
            this.Add(DefaultValue);
        }
    }
}
With usage:
Code:
BetterArray<char> myChars = new BetterArray<char>('X', 100);
foreach(char c in myChars){
    Console.WriteLine(c);
}
 
Thanks, in the end I iterated the initialisation. Listing every element in the declaration is unrealistic since there are on the order of hundreds of thousands of elements in the application.

I find the absence of a constant initialisation expression for arrays rather odd. Perhaps it was excluded intentionally to stop mistakes along the lines of accidentally initialising an array thinking it was a single variable?

Regardless, I'm going to have to rebuild the whole thing anyway since the initialisation logic elsewhere was irrevocably broken. More functions are the way forward, I think.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
89
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 10 ·
Replies
10
Views
7K
  • · Replies 25 ·
Replies
25
Views
2K