D
D
djEban2018-10-10 18:09:54
Arrays
djEban, 2018-10-10 18:09:54

C#. How can two array declarations be combined?

We need a method to combine them, but how? In one case i+2, and in the other i+1
var days = new string[30];
for (var i = 0; i < days.Length; i++)
days[i] = Convert.ToString(i + 2);
var months = new string[12];
for (var i = 0; i < months.Length; i++)
months[i] = Convert.ToString(i + 1);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sayan Batodalaev, 2018-10-13
@ya_sayanb

It's not clear how to combine them.
If you just need to join these two arrays, then:

public string[] UnionArrays(string[] days, string[] months) {
  var daysAndMonths = new string[days.Length+months.Length];
  for (var i = 0; i < days.Length; i++)
  daysAndMonths[i] = days[i];
  for (var i = 0; i < months.Length; i++)
  daysAndMonths[days.Length+i] = months[i]; 
  return daysAndMonths;
}

H
hutamari, 2018-10-16
@hutamari

Perhaps it meant:

public string[] CreateArray(int length, int step)
{
    var array = new string[length];
    for (var i = 0; i < length; i++)
        array[i] = Convert.ToString(i+step);
    return array;
}

var days = CreateArray(30,2);
var months = CreateArray(12,1);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question