call by value Call by Reference example
//Demo on Call By Reference
using System;
namespace FreshersLike
{
class CBRClass
{
public void Swap(ref int no1, ref int no2)
{
int temp;
temp = no2;
no2 = no1;
no1 = temp;
}
}
class CallByReferenceDemo
{
public static void Main()
{
CBRClass obj = new CBRClass();
int no1, no2;
Console.WriteLine("Enter two numbers : ");
no1 = int.Parse(Console.ReadLine());
no2 = int.Parse(Console.ReadLine());
Console.WriteLine("Before Swapping Values are : {0} , {1} " , no1 , no2 );
// obj.Swap(no1, no2); Error
obj.Swap(ref no1, ref no2);
Console.WriteLine("After Swapping Values are : {0} , {1} " , no1 , no2 );
}
}
}
//Demo on Call By Value
using System;
namespace FreshersLike
{
class Name
{
private string FirstName, LastName;
public void SetName(string fname, string lname)
{
FirstName = fname;
LastName = lname;
}
public string GetName()
{
string fullName;
fullName = string.Format("{0} {1}", FirstName, LastName);
return fullName;
}
}
class CallByValueDemo
{
public static void Main()
{
Name name = new Name();
name.SetName( lname: "srinivas" , fname: "sekhar" );
Console.WriteLine("Name : {0} " , name.GetName());
}
}
}
0 comments:
Post a Comment