Professional Documents
Culture Documents
27call by Value Out
27call by Value Out
27call by Value Out
com/
ketkiacharya.net@gmail.com
C# Laguage
Call By Value and Call By Reference
Ketki Acharya
From: SM VITA ATC of CDAC
9769201036
ketkiacharya.net@gmail.com
Call by value
using System;
class Test
{
/* This method causes no change to the arguments
When you pass primitive type in a function another j
used in the call. */ i
memory block get created and data get copied to public void noChange(int i, int j)
new memory block. So if you make some changes {
15 20 20 25
i += 5;
to the parameter in a function original data does not j += 5;
get affected see the diagram }
}
class CallByValue
{
public static void Main()
{
15 20 ob.noChange(a, b);
value {
public int a, b;
In call by value when you pass data it get copied
to another memory block. But when you pass public Test(int i, int j)
{
reference in a function another memory block a = i;
get created, since reference variable is holding b = j;
}
Reference(arrow) , same reference (arrow ) /* Pass an object. Now, ob.a and ob.b in object
get copied used in the call will be changed. */
So now both ob and obj pointing to same public void change(Test obj)
{
Memory bloc. obj.a += 5;
obj.b += 5;
}
obj }
class CallByRef
{
public static void Main()
{
ob a=15 20 Test ob = new Test(15, 20);
b=20 25
Console.WriteLine("ob.a and ob.b before call: " + ob.a + " " + ob.b);
ob.change(ob);
Console.WriteLine("ob.a and ob.b after call: " + ob.a + " " + ob.b);
}
}
class RefTest
When you pass primitive type in a function using {
ref /* This method changes its arguments.
Key another memory block does not get created Notice the use of ref. */
public void sqr(ref int i)
but
{
It is just another name to same memory block. i = i * i;
So if you make some changes to the parameter in a }
function original data will get modified }
see the diagram class RefDemo
Here is one important point to understand about {
ref: An argument passed by ref must be assigned a public static void Main()
{
value prior to the call. The reason is that the
RefTest ob = new RefTest();
method that receives such an argument assumes 10
a
that the parameter refers to a valid value. 100 int a = 10;
i Console.WriteLine("a before call: " + a);
class CallByRef
{
public static void Main()
{
ob Test ob = new Test(15, 20);
a=15 20 Console.WriteLine("ob.a and ob.b before call: " + ob.a + " " + ob.b);
obj b=20 25
ob.change(ref ob);
Console.WriteLine("ob.a and ob.b after call: " + ob.a + " " + ob.b);
}
}
Reference
using System;
class Decompose {
Out is also pass data by reference /* Decompose a floating-point value into its
integer and fractional parts. */
the out parameter. public int parts(double n, out double frac) {
An out parameter is similar to a ref parameter with int whole;
this one exception: It can only be used to pass a value
whole = (int) n;
out of a method.
frac = n - whole; // pass fractional part back through frac
return whole; // return integer portion
It is not necessary (or useful) to give the variable
}
used as an out parameter an initial value prior to
}
calling the method.
class UseOut {
The method will give the variable a value. public static void Main() {
using System;
class Factor
{
/* Return an array containing the factors of num.
On return, numfactors will contain the number of
factors found. */
numfactors = j;
return facts;
}
int[] }
factS
class FindFactors
{
{numfactors} public static void Main()
{
Factor f = new Factor();
numfactors int numfactors;
int[] factors;
Console.WriteLine();
}
}
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
TASK
Write a method which will accept two number and return all even
number between it as array and also give count in out variable.
Int count=
Int [] result=Dojob(5,30)
// Error! The default value for an optional arg must be known // at compile time!
namespace ConsoleAppdemo_val
{
Call by Value class Employee
{
eid=1
int a,b; public int eid;
public float sal;
e1
sal=50000
b
a=5; public Employee(int id, float salary)
{
30000
5 eid = id;
b=a; sal = salary; e2
}
a }
class Program
{
5 static void Main(string[] args)
{
Employee e1 = new
Employee(1,50000);
Employee e2;
e2 = e1;
Console.WriteLine("Hello World!");
}
}
}
using System;
using System.Collections.Generic;
using System; using System.Linq;
using System.Collections.Generic; using System.Text;
using System.Linq; using System.Threading.Tasks;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication4
{
namespace ConsoleApplication4 class Program
{ {
class Program static void Main(string[] args)
{ {
static void Main(string[] args) DisplayFancyMessage( "Wow! Very Fancy indeed!", 50, name:"raj");
{ // DisplayFancyMessage( "geeta", message: "hello",50);
DisplayFancyMessage(message: "Wow! Very Fancy indeed!", number:50, name:"raj"); Console.ReadLine();
DisplayFancyMessage(name: "geeta", number: 50, message: "hello");
Console.ReadLine(); }
} }
}
}
}
One minor gotcha regarding named arguments is that if you begin to invoke a method using
positional parameters, they must be listed before any named parameters. In other words,
named arguments must always be packed onto the end of a method call.
namespace ConsoleAppADONETSTUDENTS
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Methods *****");
DisplayFancyMessage(message: "Wow! Very Fancy indeed!",textColor: ConsoleColor.DarkRed,
backgroundColor: ConsoleColor.White);
Well, as it turns out, if you have a method that defines optional arguments, this feature can actually be really helpful.
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Methods *****");