04 Classes Methods Stmts
04 Classes Methods Stmts
04 Classes Methods Stmts
Statements
Objectives
• Classes
• Namespaces
• Methods
• Statements
Microsoft 2
Part 1
• Classes…
Microsoft 3
The class is key
Microsoft 4
Part 2
• Namespaces…
Microsoft 5
Namespaces
Microsoft 6
Definition
namespace Workshop
{ Workshop.Customer
public class Customer
{
.
.
.
}
Workshop.Product
public class Product
{
.
.
.
}
}//namespace
Microsoft 7
Example
Microsoft 8
FCL namespaces
Microsoft 9
Namespace != Assembly
• Orthogonal concepts:
– namespace for logical organization
– assembly for physical packaging
Microsoft 10
Fully-qualified references
System.Console.WriteLine("message");
• If you want, you can import a namespace & drop imported prefix
– using directive allows you to import a namespace…
using System;
.
.
.
Console.WriteLine("message");
Microsoft 11
Complete example
Microsoft 12
Point of clarification
/* main.cs */
using System;
using System.Windows;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
Microsoft 13
Part 3
• Methods…
Microsoft 14
Types of methods
Microsoft 15
Example
namespace System
{
public class Array
{
instance method public int GetUpperBound(int dimension)
(absence of static) { ... }
Microsoft 16
Calling methods
/* main.cs */
using System;
Array.Sort(data);
Microsoft 17
Parameter passing
Microsoft 18
Case 1: pass-by-value with a value type
Microsoft 19
Case 2: pass-by-ref with a value type
• Reference is passed…
Microsoft 20
Case 3: pass-by-value with a reference type
• Reference is copied…
A
public class App
{
public static void Main() Vals
{ array
Stack
int[] Vals;
Vals = new int[1000];
Vals[0] = 99;
Foo2(Vals);
System.Console.WriteLine(Vals[0]); // 100
}
• Reference to reference
is passed…
A
public class App
{
public static void Main() Vals
{ array
Stack
int[] Vals;
Vals = new int[1000];
Vals[0] = 99;
Foo2(ref Vals);
System.Console.WriteLine(Vals[0]); // 100
}
Microsoft 23
Part 4
• Statements…
Microsoft 24
Statements in C#
• Assignment
• Subroutine and function call
• Conditional
– if, switch
• Iteration
– for, while, do-while
• Control Flow
– return, break, continue, goto
Microsoft 25
Examples
x = obj.foo();
x--;
} for (int k = 0; k < 10; k++)
{
...
}
Microsoft 26
foreach
int[] data = { 1, 2, 3, 4, 5 };
int sum = 0;
Microsoft 27
Summary
Microsoft 28