Professional Documents
Culture Documents
Statements
Methods
Microsoft 2
Part 1
Statements
Microsoft 3
Statements in C#
Assignment
Subroutine and function call
Conditional
if, switch
Iteration
for, while, do-while
Control Flow
return, break, continue, goto
Microsoft 4
Examples
x = obj.foo();
x--;
} for (int k = 0; k < 10; k++)
{
...
}
Microsoft 5
Other statements
Microsoft 6
foreach
int[] data = { 1, 2, 3, 4, 5 };
int sum = 0;
Microsoft 7
using
using Workshop;
Customer c;
c = new Customer("joe hummel", 94652);
Microsoft 8
Complete example
Microsoft 9
Part 2
Methods
Microsoft 10
Types of methods
Microsoft 11
Example
namespace System
{
public class Array
{
instance method public int GetLength(int dimension)
(absence of static) { ... }
Microsoft 12
Calling methods
/* main.cs */
using System;
Array.Sort(data);
Microsoft 13
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 14
Parameter passing
Microsoft 15
Pass-by-value
int y = 9;
y unchanged F(y);
Microsoft 16
Pass-by-reference
ref parameter,
void G(ref int x)
initially 9
{
x += 1;
}
int y = 9;
Microsoft 17
Pass-by-result ("copy-out")
int y;
Microsoft 18
Summary
Microsoft 19
References
Books:
I. Pohl, "C# by Dissection"
S. Lippman, "C# Primer"
J. Mayo, "C# Unleashed"
Microsoft 20
Lab?
Microsoft 21