Home > OS >  In C#, when a variable passes through a function/method, will the original variable change?
In C#, when a variable passes through a function/method, will the original variable change?

Time:04-17

I am confused by how a function can change a variable passed through it. For example, if I created a variable t = 1, and pass a function by adding 2 to it, inside the function t is 3, but in Main function t is still 1:

static void Main(string[] args)
{
    int t = 1;
    addTwo(t);
    Console.WriteLine(t); 
    ## t=1
    Console.ReadLine();
}
static void addTwo(int t)
{
    t =2;
    Console.WriteLine("inside function {0}",t); 
    ## t=3

the value of t inside the function is 3, but in Main the value stays 1.

However if I created an array "happiness" with value {2,3,4,5,6} and passed through a function "SunIsShining" which will increase each value by 2. Afterwards, I thought the array happiness should still be {2,3,4,5,6}. However it became {4,5,6,7,8}.

static void Main(string[] args)
{
    int[] happiness = { 2, 3, 4, 5, 6 };
    SunIsShining(happiness);
    ## happiness = { 4, 5, 6, 7, 8 }

    foreach (int y in happiness)
    {
        Console.WriteLine(y);
    }
    Console.ReadLine();
}
static void SunIsShining(int[] x)
{
    for (int i = 0; i < x.Length; i  )
        x[i]  = 2;
}

Could anyone help me to understand the reason? Thank you!

CodePudding user response:

Because

  • int[] is a reference type that might pass the object reference to the function, so you modify the value from the same array.

  • int is a value type that will clone value before passing to the function, so you modify the value is not t from Main function.

We can prove by this sample code by ReferenceEquals method that will compare the object reference whether the same as below, let's say we can see addTwo is return false, but SunIsShining return true.

static int t1;
static int[] happiness;
static void Main(string[] args)
{
    t1 = 1;
    happiness = new int[]{ 2, 3, 4, 5, 6 };
    
    addTwo(t1);
    SunIsShining(happiness);
    
    Console.ReadLine();
}
static void addTwo(int t)
{   
    t =2;
    Console.WriteLine("Is same object by value?"   object.ReferenceEquals(t1,t)); 
}

static void SunIsShining(int[] x)
{
    Console.WriteLine("Is same object by refer?"   object.ReferenceEquals(happiness,x)); 
}

c# online

More information we can see

value-types

reference-types

passing-parameters

CodePudding user response:

Variable passing is divided into value passing and address passing.
Value passing can't change the previous value, address passing is passing the address, so you can change the previous value.
For example, the first one is passed by the name of the variable. T stores an int, so it passes a data, so it passes a 1 in the function, independent of the t in Main. Pass is the value
Behind; We're passing the name of the array, and the name of the array is the address of the first element in the array, so we're passing the address, so we're accessing it by address, changing the original value

  • Related