CSharp - Passing by value

Introduction

By default, arguments in C# are passed by value.

A copy of the value is created when passed to the method:

Demo

using System;

class Program//from  www . j a v  a  2  s .co  m
{
    static void Test(int p)
    {
        p = p + 1;                // Increment p by 1
        Console.WriteLine(p);    // Write p to screen
    }

    public static void Main(string[] args)
    {
        int x = 8;
        Test(x);                  // Make a copy of x
        Console.WriteLine(x);    // x will still be 8
    }
}

Result

Assigning p a new value does not change the contents of x, since p and x reside in different memory locations.