You are on page 1of 16

University of Computer Studies

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

«accessibility» «modifiers» return_type name («parameters»)


{ code... }

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.

virtual The method can be overridden by a derived class.

override The method overrides an inherited virtual or abstract method.

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.

extern The method is implemented externally, in a different language.


5
Return Type

 A method’s return_type is the type of the value the method returns.

 To return a value, use the return keyword followed by the value the method should return.

6
Method Name

 The method’s name must be a valid C# identifier.

 It should begin with a letter, underscore, or @ symbol.

 After that it can include letters, numbers, or underscores.

 If the name begins with @, it must include at least one other character.

 The name cannot include special characters such as &, %, #, and $.

 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.

 Types of Method 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.

 This keyword means the parameter is intended to be an output parameter.

 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

 useful to have methods that can take any number of parameters


 use the params keyword to make the method’s final argument a parameter array.
 A parameter array contains an arbitrary number of parameter values.
 At run time, the method can loop through the array to process the parameter values.

 Parameter arrays are subject to the following restrictions:


 A method can have only one parameter array, and it must come last in the parameter list.
 Parameter lists cannot be declared with the ref or out keywords.
 The calling code may pass the value null in the place of the parameter array.
 The calling code can provide any number of values for the parameter array including zero.
 All the items in the parameter array must have the same data type.
 However, you can use an array that contains the generic object data type and then it can hold just about anything.

13
Parameter Array

namespace ParameterArrayExample

{ class Program

{ static void Main(string[] args)

{ SendEmails ("I need C# help!", "susu@gmail.com","mama@gmail.com");

Console.WriteLine("-------------------------------------------");

SendEmails ("Hello", "susu@gmail.com","mama@gmail.com", "kaykay@gmail.com");

SendEmails("Hi",null);

Console.ReadLine();
}

14
Parameter Array

private static void SendEmails(string message, params string[] address)


{
if (address != null)
{
foreach ( string st in address)
{ Console.WriteLine("Send " + message + " to " + st); }
}
}
}
}

15
Thank You

16

You might also like