Pass Value Type

  • By Value โ‡’ Read Only
  • By Reference โ‡’ Read & Write
  • By Out โ‡’ Same as Ref, but Write first

Pass By Value

  • ุงู†ูŠ ุจู…ุฑุฑ ุงู„ู‚ูŠู…ุฉ ุจุณ ูˆู‡ูˆ ุจูŠุนู…ู„ ู†ุณุฎุฉ ุจูŠุดุชุบู„ ุนู„ูŠู‡ุง ููŠ ุงู„ function ูˆุจุนุฏ ุฃู…ุง ุจูŠุฎู„ุต ุจูŠู…ุณุญ ุงู„ู†ุณุฎุฉ ุฏูŠ ูˆู…ุด ุจูŠุบูŠุฑ ููŠ ุงู„ุฃุตู„
public static void SWAP (int X, int Y)
{
	int Temp = X;
	X = Y;
	Y = Temp;
}
 
static void Main()
{
	int A = 7, B = 3;
	SWAP(A, B); // by value ุงู„ู‚ูŠู… ู…ุด ู‡ุชุชุบูŠุฑ
}

Pass By Reference

ูˆู‡ูŠ ุงู†ูŠ ุจุจุนุช ุงู„ู…ุชุบูŠุฑ ู†ูุณู‡ ู…ุด ุจุณ ุงู„ู‚ูŠู…ุฉ ูˆุฃูŠ ุชุบูŠูŠุฑ ุจูŠุญุตู„ ุฌูˆุง ุงู„ function ุจูŠุญุตู„ ุจุฑุถูˆ ููŠ ุงู„variables ุงู„ุฃุตู„ูŠุฉ

public static void SWAP (ref int X, ref int Y)
{
	int Temp = X;
	X = Y;
	Y = Temp;
}
 
static void Main()
{
	int A = 7, B = 3;
	SWAP(ref A, ref B); // by Reference
	//A = 3, B = 7
}

Pass By Out

  • ุฒูŠ ุงู„ reference ุจุงู„ุธุจุท ุจุณ ุงู†ุช ู„ูˆ ุฌูŠุช ููŠ ุงู„ reference ุชุฏุฎู„ ู…ุซู„ู‹ุง variable ู…ุด ู…ุญุฏุฏู„ู‡ ู‚ูŠู…ุฉ ู‡ูŠุฏูŠู„ูƒ ุฅูŠุฑูˆุฑ ุนุดุงู† ุงู„ู…ูุฑูˆุถ ูŠู‚ุฑุฃ ุงู„ูƒู„ุงู… ุฏุง (Read)
  • ูุงู„ุญู„ ุงู†ูŠ ุงุณุชุฎุฏู… out
public static void SumMul(int X , int Y , out int S , out int M)
{
    S = X + Y;
    M = X * Y;
}
int A = 7, B = 3, SResult, MResult;
SumMul(A, B, out SResult, out MResult);
 
//We can define it at same call
SumMul(A, B, out int SResult, out int MResult);
 
// _ discard: ู„ูˆ ุนู†ุฏูŠ ู…ุชุบูŠุฑ ู…ุด ู‡ุณุชุฎุฏู…ู‡
SumMul(10,7, out _, out int SR);

Reference Type (Pass)

Pass By Value

  • ู‡ู†ุง ุจู‚ุง ู…ุฎุชู„ูุฉ ุงุญู†ุง ุจู†ุชุนุงู…ู„ ุฃูˆู„ ุญุงุฌุฉ ู…ุน ุงู„ heap ู…ุด ู…ุน ุงู„ stackุŒ ูู„ูˆ ุชูุชูƒุฑ ูƒู†ุง ุจู†ู‚ูˆู„ ููŠ Reference Types ุงู† ุงู†ุช ู„ูˆ ุดุงูˆุฑุช ู…ู† ref ู„ ref ุชุงู†ูŠ ุฃูƒู†ูŠ ุจุงู„ุธุจุท ุจุดุงูˆุฑ ุนู„ูŠู‡ ุฃูˆ Pointer ู„ูŠู‡ู… ู†ูุณ ุงู„ู…ูƒุงู† ููŠ ุงู„ู…ูŠู…ูˆุฑูŠ

  • ูˆุจุงู„ุชุงู„ูŠ ุฃูŠ ุชุบูŠูŠุฑ ู‡ูŠุญุตู„ ู‡ูŠุณู…ุน ููŠ ุงู„ variable ุงู„ุฃุตู„ูŠ

  • ูุฏูŠ ุจุงุฎุชุตุงุฑ ุงู†ูŠ ุจุนูŠุฏ ุชุณู…ูŠุฉ ุงู„ variable ุจุงุณู… ุชุงู†ูŠ ูˆุงู„ุชุบูŠูŠุฑ ุงู„ู„ูŠ ู‡ูŠุญุตู„ ู‡ูŠุญุตู„ ููŠ ุงู„ุฃุตู„ูŠ

  • ู„ูˆ ุบูŠุฑุช ููŠ ุงู„ู‚ูŠู…ุฉ ุจูŠุณู…ุน ููŠ ุงู„ู‚ูŠู…ุฉ ุงู„ุฃุตู„ูŠุฉ ูˆุจูŠุดุงูˆุฑูˆ ุนู„ู‰ ู†ูุณ ุงู„ุญุงุฌุฉ ููŠ ุงู„ heap

public static void RefByValue(int[] Arr)
{
    Console.WriteLine(Arr.GetHashCode()); // Same
    Arr[Arr.Length - 1] = 0;
    Console.WriteLine("In Method");
    foreach (int i in Arr) Console.WriteLine(i); // 1 0
}
 
//Main
int[] A = [1, 2];
Console.WriteLine(A.GetHashCode()); // Same
RefByValue(A);
Console.WriteLine("In Main");
foreach (int i in A) Console.WriteLine(i); // 1 0 : Changed
  • ู„ูˆ ุฎู„ูŠุชู‡ุง ุชุดุงูˆุฑ ุนู„ู‰ array ุชุงู†ูŠุฉ ู‚ูˆู„ู†ุง ู‡ุชุจู‚ุง ุฒูŠ ุงู„ pointer ุจุงู„ุธุจุท ูููŠ ุงู„ method ู‡ุชุฑูˆุญ ุชุดุงูˆุฑ ุนู„ู‰ ุญุชุฉ ุชุงู†ูŠุฉ ููŠ ุงู„ heap ุงู„ู„ูŠ ููŠู‡ุง ุงู„ array ุงู„ุฌุฏูŠุฏุฉ ุฏูŠ ูˆุฃูŠ ุชุบูŠูŠุฑ ู‡ูŠุญุตู„ ู‡ูŠุญุตู„ ุนู„ู‰ ุงู„ array ุงู„ุฌุฏูŠุฏุฉ ูˆู…ู„ูˆุด ุฃูŠ ุนู„ุงู‚ุฉ ุจุงู„ู‚ุฏูŠู…ุฉ ูˆู…ุด ู‡ูŠู„ู…ุณู‡ุง
public static void RefByValue(int[] Arr)
{
    Console.WriteLine(Arr.GetHashCode()); // Same
    int[] newArr = [5, 6, 7, 8];
    Arr = newArr;
    Arr[Arr.Length - 1] = 0; // Change at new one
    Console.WriteLine($"newArr: {Arr.GetHashCode()}"); // diff
    Console.WriteLine("In Method");
    foreach (int i in Arr) Console.WriteLine(i); // new one
}
 
//Main
int[] A = [1, 2];
RefByValue(A);
Console.WriteLine(A.GetHashCode());
Console.WriteLine("In Main");
foreach (int i in A) Console.WriteLine(i);

Pass By Reference

  • ู‡ุชุจู‚ู‰ ู†ูุณ ุงู„ุญูˆุงุฑ ููŠ ุงู„ู„ูŠ ููˆู‚ ู„ูˆ ุบูŠุฑุช ููŠ ุงู„ู‚ูŠู…ุฉ
  • ุงู„ูุฑู‚ ู‡ูŠุธู‡ุฑ ุงู†ูŠ ู„ูˆ ุฎู„ูŠุช ุงู„ array ุชุดุงูˆุฑ ุนู„ู‰ ูˆุงุญุฏุฉ ุชุงู†ูŠุฉ ููŠ ุงู„ุญุงู„ุฉ ุฏูŠ ุงู„ุฃุตู„ูŠุฉ ูˆุงู„ู„ูŠ ููŠ ุงู„ method ุงู„ุงุชู†ูŠู† ู‡ูŠุฑูˆุญูˆุง ูŠุดุงูˆุฑูˆ ุนู„ู‰ ุงู„ุฌุฏูŠุฏุฉ ุฏูŠ ูŠุนู†ูŠ ุงู„ array ุงู„ main ูƒู…ุงู† ู‡ุชุชุบูŠุฑ ูˆุชุจู‚ุง ุจุชุดุงูˆุฑ ููŠ ุงู„ heap ุนู„ู‰ ุงู„ุฌุฏูŠุฏุฉ ุงู„ู„ูŠ ุบูŠุฑุชู‡ุง ููŠ ุงู„ method
public static void RefByValue(ref int[] Arr)
{
    Console.WriteLine(Arr.GetHashCode());
    int[] newArr = [5, 6, 7, 8];
    Arr = newArr; // The main one will be changed
    Arr[Arr.Length - 1] = 0;
    Console.WriteLine($"newArr: {Arr.GetHashCode()}");
    Console.WriteLine("In Method");
    foreach (int i in Arr) Console.WriteLine(i);
}
 
//Main
int[] A = [1, 2];
Console.WriteLine(A.GetHashCode());
RefByValue(ref A);
Console.WriteLine(A.GetHashCode());
Console.WriteLine("In Main");
foreach (int i in A) Console.WriteLine(i);