Methods in C# - 051404
Methods in C# - 051404
● Methods in C#
● Characteristics Of A Method
● Advantages Of Using Method
● Method Invocation or Method Calling
● Passing Parameters to a Method
○ Value Parameters
○ Reference Parameters
○ Output Parameters
Methods In C#
Methods are a group of statements that can be put together to perform a specific operation and return the
result to the calling method section and also helps reduce writing the same code multiple times. It also helps
in code reusability. Every C# program has at least one method that is the popular Main(). There are two
significant parts of a method you need to place in a program to execute them successfully. These are:
● Return type
● Method name
● Parameter list
Advantages Of Using Method
● It provides an effective way for the user to reuse the existing code.
● Value parameters
● Reference parameters
● Output parameters
Value Parameters
● A new storage location is created for every value parameter.
● This is the default approach to pass parameters to a method.
● When a value- type variable is passed to a method it means that we are passing a
copy of the variable to the method. So the method does not have any effects
outside the method.
Example of Value Parameters
using System; p.Addition(2, 5);
namespace Tutpoint Console.WriteLine("Result: " + result);
{ Console.ReadKey();
class Program }
{ public void Addition(int a, int b)
static void Main(string[] args) {
{ int result = a + b;
int result; Console.WriteLine("Result inside method: " + result);
int a = 25; }
int b = 75; }
result = a + b; }
Console.WriteLine("Result: " + result);
OUTPUT: Result: 100
Program p = new Program(); Result inside method: 7
Result: 100
Reference Parameters
● Unlike Value-type, no new storage location is created for reference parameters. In
spite of it, a reference to the same memory location of a variable is said to be a
reference parameter.
● ref keyword is used to declare reference type variable on method. It is also
required while making a call to a method.
● When a Reference- type variable is passed to a method it means that we are
passing the reference to the same memory location of a variable as that of actual
parameters. So when a change is made in a method, the change also gets reflects
outside of it.
Example of Reference Parameters
using System; public void StringSwapping(ref int c, ref int d)
namespace Tutpoint {
{ int temp;
class Program temp = d;
{ d = c;
static void Main(string[] args) c = temp;
{ Console.WriteLine("Inside method: Value of a= " + c +
int a = 25; " and b= " + d);
int b = 75; }
Console.WriteLine("Value of a= " + a + " and b= " + b); }
Program p = new Program(); }
p.StringSwapping(ref a, ref b);
Console.WriteLine("Value of a= " + a + " and b= " + b); OUTPUT: Value of a= 25 and b= 75
Console.ReadKey(); Inside method: Value of a= 75 and b= 25
} Value of a= 75 and b= 25
Output Parameters
● Output parameters are used to return more than one value from a method. From
return keyword, we are only able to return one value.
● out keyword is used to declare Output type parameters on a method. It is also
required while making a call to a method.
● This is similar to reference-type except that multiple values are returned from the
method.
Example of Output Parameters
using System; public void ValueOutput(out int c, out int d)
namespace Tutpoint {
{ d = 100;
class Program c = 1000;
{ Console.WriteLine("Inside method: Value of c= " + c + "
static void Main(string[] args) and d= " + d);
{ }
int a = 25; }
int b = 75; }
Console.WriteLine("Value of a= " + a + " and b= " + b);
Program p = new Program();
p.ValueOutput(out a, out b);
Console.WriteLine("Value of a= " + a + " and b= " + b); OUTPUT: Value of a= 25 and b= 75
Console.ReadKey(); Inside method: Value of c= 1000 and d= 100
} Value of a= 1000 and b= 100
●THANK YOU