当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to declare the same string value multiple times inside an array?

问题描述:

My question is in relation to multiple array values that are the same within C#.

As a rosary has five decades, I want to make values "arr[1,2,...10, 14,16,...23]="Hail Mary";" I could just write each value out in the string array, but feel reducing the size of code would make the program more efficient in limiting its size. Most questions that deal with repetitious values tend to be asked to avoid such, but this deals with different circumstances and a different type such as int. Mine is just the opposite where I am using string and desire to repeat the same value in multiple index numbers. I think there can be multiple uses such as a windows form program that repeats step-by-step instructions to a user to follow that might display a repeated string value.

1)is there a way to code an array with multiple same values, or do I just write each value out as my example already does?

2)If using string[] arr={"Our Father", "Hail Mary", "Hail Mary",..., "Glory Be", "O My Jesus"}; instead of my example that declares each indexed string value, how would it be coded to insert the same value inside the declared string array?

int counter=0;

private void button1_Click(Object sender, EventArgs e)

{ string[] arr= new string[12];

arr[0]="Our Father...";

arr[1]="Hail Mary...";

arr[2]="Hail Mary...";

...

arr[11]="Glory Be...";

arr[12]="O My Jesus...";

textBox1.Text = arr[counter];

counter++;

textBox1.Text = arr[counter-1].ToString();

}

网友答案:

You can use switch statement for such purpose:

string[] arr= new string[23];
for (int i = 0; i < arr.Length; i++) {
    switch (i) {
    case 0:
        array[i] = "Our Father...";
        break;
    case 11:
        arr[11]="Glory Be...";
        break;
    case 11:
        arr[12]="O My Jesus...";
        break;
    default:
        array[i] = "Hail Mary";
        break;
    }
}

The sense is that the most common entry would be "Hail Mary", you put it as a default case and process the rest of the cases separately.

Note also I'm using arr.Length instead of just number; I'd recommend you to always do that as a good practice (to not repeat same constants over and over).

网友答案:

Use Enumerable.Repeat method for this

var array = Enumerable.Repeat("Hail Mary", count).ToArray();

This will create an array of count items where each of them will be equal to "Hail Mary". Then just place different values where they should be.

array[0] = "Our Father";

For more information about this method check out MSDN.

网友答案:

Maybe you can write a simple method for your scenario:

public void SetValuee(int start,int end,string value,ref string[] array)
{
   for(int i=start;i<=end;i++)
   { 
       array[i] = value;
   }
}

And use:

SetValues(1,12,"Hail Mary...",ref arr)
网友答案:
 string[] arr = { "Our Father", "Hail Mary", "Hail Mary", "Glory Be", "O My Jesus" };
 var list = arr.ToList();
 list.Insert(2, "Hail Mary");
 arr = list.ToArray();
网友答案:

Yet another answer, you could use a second array and a foreach loop

int[] index = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
string[] arr = new string[13];

foreach (int i in index)
{
    arr[i] = "Hail Mary...";
}
arr[0] = "Our Father...";
arr[11] = "Glory Be...";
arr[12] = "O My Jesus...";
网友答案:

No need to overcomplicate things, just assign the value to a string variable and then use it when you declare the array. you will not gain anything in performance by somehow reducing the size of source code, or by using a "clever" method that would populate your array. If there are thousands or more places where you'd need to put your string that repeats, you could perhaps gain a few milliseconds, but you'd lose in clarity of what you're doing.

So, for the second part of your question, you could declare the array like this:

string repeatedString = "I'm repeating";
string[] arr = 
{ 
    "Value 1", 
    "Another string", 
    repeatedString, 
    "some more", 
    repeatedString, 
    repeatedString, 
    "Yet another" 
};
分享给朋友:
您可能感兴趣的文章:
随机阅读: