Passing ref-types by value : Parameter Value « Language Basics « C# / CSharp Tutorial






using System;

class Person
{
  public string fullName;
  public int age;

  public Person(string n, int a)
  {
    fullName = n;
    age = a;
  }

  public void PrintInfo()
  {
    Console.WriteLine("{0} is {1} years old", fullName, age);
  }
}

class MainClass
{
  public static void SendAPersonByValue(Person p)
  {
    p.age = 99;

    p = new Person("TOM", 999);
  }

  public static void Main() 
  {
    Person fred = new Person("Fred", 12);
    fred.PrintInfo();
    SendAPersonByValue(fred);
    fred.PrintInfo();
  }
}
Fred is 12 years old
Fred is 99 years old








1.14.Parameter Value
1.14.1.Passing ref-types by value
1.14.2.Value types are passed by value