0% found this document useful (0 votes)
40 views14 pages

Methods in C# - 051404

The document discusses different types of parameters that can be passed to methods in C#: value parameters, reference parameters, and output parameters. It provides examples of each type and explains how they allow passing values into methods and returning values.

Uploaded by

Ashim Basumatary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views14 pages

Methods in C# - 051404

The document discusses different types of parameters that can be passed to methods in C#: value parameters, reference parameters, and output parameters. It provides examples of each type and explains how they allow passing values into methods and returning values.

Uploaded by

Ashim Basumatary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Contents

● 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:

● The method definition


● The method Call

The syntax for defining a method in C# is as follows −


<Access Specifier> <Return Type> <Method Name>(Parameter List)
{
Method Body
}
Characteristics Of A Method
● Access Specifier

● Return type

● Method name

● Body of the method

● Parameter list
Advantages Of Using Method

● It makes the program well structured.

● Methods enhance the readability of the code.

● It provides an effective way for the user to reuse the existing code.

● It optimizes the execution time and memory space.


Method Invocation or Method Calling
Method Invocation or Method Calling is done when the user wants to execute the method.
The method needs to be called for using its functionality. A method returns to the code that
invoked it when:
● It completes all the statements in the method
● It reaches a return statement
● Throws an exception
Example
using System; static void Main(string[] args)
namespace Application { {
class Methods { int a = 12;
static int Sum(int x, int y) int b = 23;
{ int c = Sum(a, b);
int a = x; Console.WriteLine("The Value of the sum is " + c);
int b = y }
int result = a + b; }
return result; }
}

Output : The Value of the sum is 35


Passing Parameters to a Method
When method with parameters is called, you need to pass the parameters to the
method. There are major three ways that parameters can be passed to a method −

● 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

You might also like