Advertisement
//C# Calling a method with argument, by value
using System;
class A
{
//Defining the method Add() to add 10 to the int value passed to it
void Add(int i)
{
i = i + 10;
}
//Defining the Main() method
public static void Main(String[] ar)
{
//A local variable
int a = 10;
//Creating an object of A class
A ob = new A();
//Printing the value of local variable,a
//Before calling the Add() method
Console.WriteLine("Before calling the Add() method, the value in a is : " + a);
//The Add() method is called by value, which means
//the value of variable a is passed to it by value.
ob.Add(a);
//Printing the value of local variable,a
//After calling the Add() method
Console.WriteLine("Afrer calling the Add() method, the value in a is : " + a);
}
}
Before calling the Add() method, the value in a is : 10
Afrer calling the Add() method, the value in a is : 10
// Add() method is called by value, which means
//the value of variable a is passed to it by value.
ob.Add(a);
//Defining the method Add() to add 10 to the int value passed to it
void Add(int i)
{
i = i + 10;
}
Advertisement
//C# Example of calling a method by values to call
//a method with arguments which swaps or interchanges the value in two char variables
using System;
class A
{
//Defining the Swap_Char() method to swap characters
void Swap_Char(char c, char d)
{
char e;
e = c;
c = d;
d = e;
}
//Defining the Main() method
public static void Main()
{
//Creating an object of class A
A ob = new A();
//Two local char variables
char a ='x';
char b ='y';
//Printing the local char variables before calling the Swap_Char() method
Console.WriteLine("The character value in a is : " + a);
Console.WriteLine("The character value in b is : " + a);
//Calling the Swap_Char() method of class A
ob.Swap_Char(a,b);
Console.WriteLine("After the Swap_Char() method is called");
//Printing the local char variables after calling the Swap_Char() method
Console.WriteLine("The character value in a is : " + a);
Console.WriteLine("The character value in b is : " + b);
}
}
The character value in a is : x
The character value in b is : x
After the Swap_Char() method is called
The character value in a is : x
The character value in b is : y
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement