How to change the value of int array in method with C#

2 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] arr = new int[3];

            arr[0] = 13;
            arr[1] = 340;
            arr[2] = 1299;

            SetValue(arr);

            foreach (int n in arr)
                Console.WriteLine(n);
        }

        static void SetValue(int[] arr)
        {
            if (arr != null && arr.Length > 0)
                arr[2] = 100;
        }

    }
}


/*
run:
      
13
340
100

*/

 



answered Dec 28, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] arr = new int[3];

            arr[0] = 13;
            arr[1] = 340;
            arr[2] = 1299;

            SetValue(arr, 2, 16000);

            foreach (int n in arr)
                Console.WriteLine(n);
        }

        static void SetValue(int[] arr, int index, int value)
        {
            if (arr != null && arr.Length > 0)
                arr[index] = value;
        }

    }
}


/*
run:
      
13
340
16000

*/

 



answered Dec 28, 2016 by avibootz
...