Professional Documents
Culture Documents
Methods
Methods
Chapter (4)
Methods
1
Contents
Method Syntax
Accessibility
Modifiers
Method Name
Return type
Parameters
Pass by Value
Pass by Reference
out Parameter
Parameter Arrays
2
Method Syntax
a group of statements that together perform a task
a block of code which only runs when it is called
pass data, known as parameters, into a method
used to perform certain actions, and they are also known as functions
To reuse code: define the code once, and use it many times
To use a method, need to
Define the method
Call the method
Syntax
3
Accessibility
Public The method can be accessed from anywhere, including outside the class.
protected The method can be accessed from within the class to which it belongs, or a type derived from
that class.
internal The method can be accessed from within the same program.
protected The method can accessed in the same assembly or in a derived class in other assemblies
internal (projects).
private The method can only be accessed from inside the class to which it belongs.
private protected The method can be accessed inside the containing class or in a class that derives from a
containing class, but only in the same assembly(project).
4
Modifier
Modifier Description
new The method hides an inherited method with the same signature.
static The method does not operate on a specific instance of the class.
sealed The method overrides an inherited virtual method, but cannot be overridden by any classes
which inherit from this class. Must be used in conjunction with override.
abstract A virtual method which defines the signature of the method, but doesn’t provide an
implementation.
To return a value, use the return keyword followed by the value the method should return.
6
Method Name
If the name begins with @, it must include at least one other character.
It also cannot be the same as C# keywords such as if, for, and public.
7
Parameters
Enclosed between parentheses, the parameters are used to pass and receive data from a method.
A method’s parameter declaration defines the names and types of the parameters passed into it
Parameters are optional; that is, a method may contain no parameters.
Pass by Value
Pass by Reference
Out Parameter
8
Pass by Value
That means the method receives a copy of the parameter’s value.
Example
using System; Console.WriteLine("After swap, value of a : "+ a);
namespace PassByValueExample { Console.WriteLine("After swap, value of b : "+ b);
class Program Console.ReadLine();
{ static void Main(string[] args)
{ }
Program S = new Program();
int a = 100; public void swap(int x, int y)
int b = 200; { int temp;
Console.WriteLine("Before swap, value of a : " +a); temp = x;
x = y;
Console.WriteLine("Before swap, value of b : " + b); y = temp;
}
S.swap(a, b); }
}
9
Pass by Reference
The method receives a reference to the argument’s value, not a copy of the value.
Example
using System; Console.WriteLine("After swap, value of a : "+ a);
namespace PassByRefExample{
class Program Console.WriteLine("After swap, value of b : "+ b);
{ static void Main(string[] args)
{ Console.ReadLine();
}
Program S = new Program();
public void swap (ref int x,ref int y)
int a = 100; int b = 200; { int temp;
Console.WriteLine("Before swap, value of a : " +a); temp = x;
x = y;
Console.WriteLine("Before swap, value of b : " + b); y = temp;
}
S.swap(ref a,ref b); }
}
10
Out Parameter
The final way you can pass a value into a method uses the out keyword.
The argument is passed into the method by reference so the method can set its value.
The method does not assume the value has been initialized before it is passed into the method, and the method
assigns a value to the parameter before it returns.
11
Out Parameter
Example
namespace Eg {
class Program
{ static void Main(string[] args)
{
Program obj = new Program ();
int value;
obj.AssignValue(out value);
Console.WriteLine("Value : " + value.ToString());
Console.ReadLine();
}
private void AssignValue(out int number)
{
number = 50;
Console.WriteLine("Number : " + number.ToString());
} }}
12
Parameter Array
13
Parameter Array
namespace ParameterArrayExample
{ class Program
Console.WriteLine("-------------------------------------------");
SendEmails("Hi",null);
Console.ReadLine();
}
14
Parameter Array
15
Thank You
16