[Solved] How to return an array in C# without creating array inside the method? [closed]


static IEnumerable<int> Number(int number1, int number2){
    for(int i=number1; i<=number2; i++) {
        if(i%2==1) {
            yield return i;
        }
    }
}

or

static int[] Number(int number1, int number2){
    var x = new int[number2];
    int y = 0;
    for(int i=number1; i<=number2; i++) {
        if(i%2==1) {
            x[y] = i;
            y++;
        }
    }
    return x;
}

This is what you are looking for without creating an array

Call it like:

var something = Number(1,5).ToArray();

19

solved How to return an array in C# without creating array inside the method? [closed]