1 st Semester Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
1 st Semester /21 Outline C# Method Review Parameter Passing Pass by reference Pass by value
1 st Semester /21 C# Structure – Multiple MethodsNamespace Class Main() Method1() Method2() Method3() MethodNN()
1 st Semester /21 Method Types Method Type PredefinedMethodUser-definedMethod Returnvalue No return valueReturnvalue value
1 st Semester /21 Method Types Method Type PredefinedMethodUser-definedMethod Returnvalue No return valueReturnvalue value
1 st Semester /21 Method Declaration static ( ) { ; } #remark return-type can be - data type = int, double, string, … need return statement need return statement - void = return no value
1 st Semester /21 Method Example: with return value … int x = power2(2); … static int power2(int n) { int j = n * n; return j; int j = n * n; return j;} *Methods can return only single value.
1 st Semester /21 Method Example: without return value … DisplayName(”Aphirak”); … static void DisplayName(string s){ Console.WriteLine( ” Hello, {0}”,s); Console.WriteLine( ” Hello, {0}”,s);}
1 st Semester /21 Method Example
1 st Semester /21 Outline C# Method Review Passing Parameter Pass by value Pass by reference
1 st Semester /21 Passing Parameter Example 1 … int x = power2(2); … static int power2(int n) { int j = n * n; return j; int j = n * n; return j;}
1 st Semester /21 Passing Parameter Example 2 … int x = power2(2); … static int power2(int n) { n = n * n; return n; n = n * n; return n;}
1 st Semester /21 Passing Parameter Type Passing Type Pass by value Pass by reference
1 st Semester /21 Outline C# Method Review Passing Parameter Pass by value Pass by reference
1 st Semester /21 Pass by value Example … string s; s = ”InitialD”; DisplayMovie(s); Console.WriteLine(s); … static void DisplayMovie(string st) { Console.WriteLine(”Movie = {0}”,st) Console.WriteLine(”Movie = {0}”,st) st = ”TomYumKung”; st = ”TomYumKung”;} Copy value s to st
1 st Semester /21 Outline C# Method Review Passing Parameter Pass by value Pass by reference
1 st Semester /21 Pass by reference Example … string s; s = ”InitialD”; DisplayMovie(ref s); Console.WriteLine(s); … static void DisplayMovie( ref string st) { Console.WriteLine(”Movie = {0}”,st) Console.WriteLine(”Movie = {0}”,st) st = ”TomYumKung”; st = ”TomYumKung”;} s is referred by st Changes st/changes s too
1 st Semester /21 How to write method: swap x and y x = 5; y = 4; swap( x, y); Console.WriteLine(”x={0}, y={1}”,x,y); swap(ref x, ref y);
1 st Semester /21 Keywords ref and out C# also provides another pass by reference keyword: out ref & out: static void Add(int a, int b, out int c) { c = a + b; } refout un-initialized variables are not allowed any variables are allowed previous values are passed to methods no previous values are passed to methods
1 st Semester /21 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
1 st Semester /21 Summary Method Return Value: int, double, etc… void Passing Parameter Pass by value Pass by reference