Passing arguments

Passing arguments

What is the output of the following code?

using System;

class A
{
    public int x = 25;
}

class Program
{
    static void Main(string[] args)
    {
        int x = 25;
        f1(x);
        Console.WriteLine(x);

        int[] a = {25};
        f2(a);
        Console.WriteLine(a[0]);

        A obj = new A();
        f3(obj);
        Console.WriteLine(obj.x);
    }

    static void f1(int x)
    {
        x = x + 5;
    }

    static void f2(int[] x)
    {
        x[0] = x[0] + 5;
    }

    static void f3(A obj)
    {
        obj.x = obj.x + 5;
    }
}

Primitive types are passed by value and non-primitive types are passed by reference!

Last updated