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

Discussion Overview

The discussion revolves around how to set every element of an array to the same value in C#. Participants explore various methods, including initialization techniques and potential workarounds, while expressing frustration over the limitations of the language.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in setting all elements of an array to the same value in a single line, questioning if iteration is necessary.
  • Another participant suggests that while shorthand initialization is possible with specific values, it does not meet the original query's requirement for setting all elements to a single value without iteration.
  • A third participant proposes creating a custom class that wraps around an array or list to facilitate initialization with a default value, providing a code example.
  • A later reply indicates that the participant ultimately resorted to iteration for initialization, noting the impracticality of listing every element for large arrays and expressing confusion over the lack of a constant initialization expression for arrays.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a single method to achieve the desired outcome without iteration, and multiple approaches are discussed without agreement on their effectiveness.

Contextual Notes

Participants mention limitations related to the syntax and initialization capabilities of C#, as well as the potential for confusion in array initialization versus single variable assignment.

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.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
89
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · 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 25 ·
Replies
25
Views
3K
  • · Replies 10 ·
Replies
10
Views
7K