Call by Value Call by Reference and Params
Concept in C# .Net...................
Call by value: In the
call by value method, the called function creates a new set of variables in
stack and copies the values of the arguments into them.
Example: Program showing the Call by Value method..........................
First we will create class which name is
Class1 and defined method Swap with two no...
class Class1
{
public void Swap(int a, int b)
{
a = a +
b;
b = a -
b;
a = a -
b;
MessageBox.Show("Swapped
values are a = " + a + "and b =
" + b);
}
}
Then we will take a Window From with two TextBox ………………………………
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CallByValueandReference
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int a, b;
Class1 cc = new Class1();
private void
btnswap_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(textBox1.Text);
b = Convert.ToInt32(textBox2.Text);
MessageBox.Show("Original
values are a = "+ a +"and b="+
b);
cc.Swap(
a, b);
MessageBox.Show("The
values after swap are a = " + a + "and
b =" + b);
}
}
}
Output:
Call by reference: In the
call by reference method, instead of passing values to the function being
called, references/pointers to the original variables are passed.
Example: Program showing the Call by Reference method.........................
First we will create class which name is
Class1 and defined method Swap with two no.........
class Class1
{
public void Swap(ref int a , ref int b)
{
a = a +
b;
b = a -
b;
a = a -
b;
MessageBox.Show("Swapped
values are a = " + a + "and b =
" + b);
}
Then we will take a Window From with two TextBox ………………………………
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CallByValueandReference
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int a, b;
Class1 cc = new Class1();
private void
btnswap_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(textBox1.Text);
b = Convert.ToInt32(textBox2.Text);
MessageBox.Show("Original
values are a = "+ a +"and b="+
b);
cc.Swap(ref a ,ref b);
MessageBox.Show("The
values after swap are a = " + a + "and
b =" + b);
}
}
}
Output:
What is Params : its enables
methods to receive variable numbers of parameters. With params, the arguments
passed to a method are changed by the compiler to elements in a temporary array. In other way we can say you are not
assured about number of parameters or you want to create a method that can
accept n number of parameters at run time. This situation can be handled with
params type array in C#. The params keyword creates an array at run time that
receives and holds n number of parameters.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CallByValueandReference
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
class Class1
{
// params concept in C# .net
public int sum(params int[] arr)
{
int total = 0;
foreach (int a in arr)
total
+= a;
return total;
}
}
private void
button1_Click(object sender, EventArgs e)
{
Class1 cc = new Class1();
int res = cc.sum(2, 8, 9, 08, 34, 23);
MessageBox.Show(res.ToString());
}
}
}
Output:
0 comments:
Post a Comment