Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 82.9k

what's the best way?

Feb 4 2023 2:18 PM

Hi

Both codes gives 10 20 and 30. I'm just wondering which one is the best (efficient)? Thanks. V.

First way:

using System;
class X
{
    static void Main(string[] args)
    {
        int[] s = new int[3];
            s[0] = 1; s[1] = 2; s[2] = 3;

        for (int i = 0; i < s.Length; i++)
            Console.WriteLine(p(s,i)[i]);
    }

    static int[] p(int[] sl, int x)
    {
        sl[x] *= 10;
        return sl;
    }
}

Second way:

using System;
class X
{
    static void Main(string[] args)
    {
        int[] s = new int[3];
        int[] s2 = new int[3];
        s[0] = 1; s[1] = 2; s[2] = 3;
                
        s2 = p(s);
        for (int i = 0; i < s.Length; i++)
            Console.WriteLine(s2[i]);
    }

    static int[] p(int[] sl)
    {
        for (int i = 0; i < sl.Length; i++)
            sl[i] *= 10;
        return sl;
    }
}
 


Answers (4)