You are on page 1of 24

Working with Methods in C#

Using Methods

Access Modifiers Defining Methods Calling Methods Using the return Statement Using Local Variables Returning Values Encapsulation Data with Properties

Method Types
Method Type Predefined Method User-defined Method

Return value

No return value

Return value

No return value

Method Types
Method Type Predefined Method User-defined Method

Return value

No return value

Return value

No return value

Method Declaration
static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; }

#remark return-type can be - data type = int, double, string, need return statement - void = return no value

Method Example: with return value

*Methods can return only single value.


int x = power2(2);

static int power2(int n) { int j = n * n; return j; }

Method Example: without return value


DisplayName(Aphirak);

static void DisplayName(string s){ Console.WriteLine(Hello, {0},s); }

Method Example 1
0 3 4

1 2

Outline
C# Method Review Passing Parameter
Pass by value Pass by reference

Passing Parameter Example 1


int x = power2(2);

static int power2(int n) { int j = n * n; return j; }

Passing Parameter Example 2


int x = power2(2);

static int power2(int n) { n = n * n; return n; }

Passing Parameter Type


Passing Type Pass by reference

Pass by value

Outline
C# Method Review Passing Parameter
Pass by value Pass by reference

Pass by value Example


string s; Copy value s to st s = InitialD; DisplayMovie(s); Console.WriteLine(s);
static void DisplayMovie(string st) { Console.WriteLine(Movie = {0},st) st = TomYumKung; }

Outline
C# Method Review Passing Parameter
Pass by value Pass by reference

Pass by reference Example


string s; s is referred by st s = InitialD; DisplayMovie(ref s); Console.WriteLine(s); Changes st/changes s too
static void DisplayMovie(ref string st) { Console.WriteLine(Movie = {0},st) st = TomYumKung; }

How to write method: swap x and y


x = 5; y = 4; swap( x, y); swap(ref x, ref y); Console.WriteLine(x={0}, y={1},x ,y);

Keywords ref and out


C# also provides another pass by reference keyword: out

static void Add(int a, int b, out int c) { ref & out: c = a + b; }

ref
un-initialized variables are not allowed previous values are passed to

out
any variables are allowed

no previous values are passed

methods

to methods

Example
Min(x ,y) Find min value Max(x, y) Find max value Mul(x, y) Find x * y IsPrime(x) Is x a prime number? CircleArea(r) Find area or circle with radius r BahtToDollar(b) Convert baht to dollar

Encapsulating data with Properties


Data encapsulation and hiding are the two fundamental characteristics of any object oriented programming language In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members. Properties are nothing but natural extension of data fields

can be any Properties in C# C# valid can be private, public, protected or internal. type

Properties are defined using the property declaration syntax


<acces_modifier> <return_type> <property_name> { get { } set { } }

Properties

class MyClass { private int x; public int X { get { return x; } set { x = value; } }

Summary
Method
Return Value: int, double, etc void

Passing Parameter
Pass by value Pass by reference

You might also like