04 Methods Stmts
04 Methods Stmts
Statements in C#
• Assignment
• Subroutine and function call
• Conditional
– if, switch
• Iteration
– for, while, do-while
• Control Flow
– return, break, continue, goto
Microsoft 2
Examples
x = obj.foo();
x--;
} for (int k = 0; k < 10; k++)
{
...
}
Microsoft 3
Other statements
Microsoft 4
foreach
int[] data = { 1, 2, 3, 4, 5 };
int sum = 0;
Microsoft 5
using
using Workshop;
Customer c;
c = new Customer("joe hummel", 94652);
Microsoft 6
Complete example
Microsoft 7
Types of methods
Microsoft 8
Example
namespace System
{
public class Array
{
instance method public int GetLength(int dimension)
(absence of static) { ... }
Microsoft 9
Calling methods
/* main.cs */
using System;
Array.Sort(data);
Microsoft 10
Other useful static methods
• A program to add 2 integers and output the sum:
using System;
sum = a + b;
output = String.Format("{0} + {1} = {2}", a, b, sum);
Console.WriteLine(output);
}
}
Microsoft 11
Parameter passing
Microsoft 12
Pass-by-value
int y = 9;
y unchanged F(y);
Microsoft 13
Pass-by-reference
ref parameter,
void G(ref int x)
initially 9
{
x += 1;
}
int y = 9;
Microsoft 14
Pass-by-result ("copy-out")
int y;
Microsoft 15