Classes, Methods and Statements
Objectives “With regards to programming classes, methods and statements, C# offers what you would come to expect from a modern OOPL…”
• • • •
Classes Namespaces Methods Statements
Microsoft
2
Part 1 •
Classes…
Microsoft
3
The class is key •
In .NET, classes play a central role – every type is represented by a class – all data and code must reside within a class
public class App { public static void Main() { . . . } }
Microsoft
public class Customer { . . . } public class Globals { . . . } public class Utility { . . . } 4
Part 2 •
Namespaces…
Microsoft
5
Namespaces • •
The FCL contains thousands of classes… Inevitably, name collisions will occur – between classes in the FCL – between your classes and those in the FCL
•
Namespaces are a way to minimize collisions… – as well as logically organize our code
Microsoft
6
Definition •
A namespace N is a set of names qualified by N namespace Workshop { public class Customer { . . . } public class Product { . . . } }//namespace
Microsoft
Workshop.Custome r
Workshop.Produc t
7
Example •
Framework Class Library (FCL) contains 1000's of classes – organized by namespaces
Microsoft
8
FCL namespaces • •
FCL's outermost namespace is "System" FCL technologies nested within System… Namespace
Purpose
Assembly
System
Core classes, types
mscorlib.dll
System.Collections
Data structures
mscorlib.dll
System.Data
Database access
System.Data.dll
System.Windows.Forms
GUI
System.Windows.Forms.dll
System.XML
XML processing
System.Xml.dll
Microsoft
9
Namespace != Assembly •
Orthogonal concepts: – namespace for logical organization – assembly for physical packaging
• •
You must reference an assembly in order to use it You can "import" a namespace to reduce typing…
Microsoft
10
Fully-qualified references • A fully-qualified reference starts with the outermost namespace: 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 •
using directive(s) specified at top of file
/* main.cs */
namespace Workshop { public class Customer { . . . }
using System; using Workshop; public class App { public static void Main() { Customer c; c = new Customer("jim bag", 94652); Console.WriteLine( c.ToString() ); } }
Microsoft
public class Product { . . . } }
12
Point of clarification • •
using directive only includes types from specified namespace nested namespaces must be separately imported... /* main.cs */ using using using using using
System; System.Windows; System.Windows.Forms; System.Data; System.Data.OleDb;
public class App { . . . }
Microsoft
13
Part 3 •
Methods…
Microsoft
14
Types of methods •
Classes contain 2 types of methods: – subroutines with no return value (void) – functions with a return value (int, string, etc.)
•
Methods may be: – instance – static
• •
Instance methods require an object to call Static methods are global and thus require only class name
Microsoft
15
Example •
Array class in FCL – fully-qualified name is System.Array
instance method (absence of static)
namespace System { public class Array { public int GetUpperBound(int dimension) { ... } public static void Sort(Array a) { ... }
static method (presence of static)
. . . }
Microsoft
}
16
Calling methods •
Here's an example of calling into the Array class: /* main.cs */ using System; public class App { public static void Main() { int[] data = { 11, 7, 38, 55, 3 }; Array.Sort(data); for (int i=0; i<=data.GetUpperBound(0); i++) Console.WriteLine(i + ": " + data[i]); } }
Microsoft
17
Parameter passing •
C# offers three options: – pass-by-value (default) – pass-by-reference – pass-by-result ("copy-out")
•
More subtle than you might think…
Microsoft
18
Case 1: pass-by-value with a value type •
Bits are copied… value
99
i
99
public class App
{
public static void Main() { int i = 99; Foo(i); System.Console.WriteLine(i); }
stack frame for Foo
stack frame for Main
Stack
// i = 99
private static void Foo(int value) { value = value + 1; } }
Microsoft
19
Case 2: pass-by-ref with a value type •
Reference is passed… stack frame for Foo
value
public class App
{
i
public static void Main() { int i = 99; Foo(ref i); System.Console.WriteLine(i); }
99
stack frame for Main
Stack
// i = 100
private static void Foo(ref int value) { value = value + 1; } }
Microsoft
20
Case 3: pass-by-value with a reference type •
Reference is copied… A
public class App
{
public static void Main() Vals { Stack int[] Vals; Vals = new int[1000]; Vals[0] = 99; Foo2(Vals); System.Console.WriteLine(Vals[0]); // 100 }
array
private static void Foo2(int[] A) { A[0] = A[0] + 1; } }
Microsoft
21
Case 4: pass-by-ref with a reference type •
Reference to reference is passed… A
public class App
{
public static void Main() Vals { Stack int[] Vals; Vals = new int[1000]; Vals[0] = 99; Foo2(ref Vals); System.Console.WriteLine(Vals[0]); // 100 }
array
private static void Foo2(ref int[] A) { A[0] = A[0] + 1; } }
Microsoft
22
Case 5: pass-by-result? •
Pass-by-result is identical to pass-by-ref, except: – no value is passed in – result is copied back upon method return public class App
{
public static void Main() { int a, b; ComputeXYZ(out a, out b); System.Console.WriteLine("Results: " + a + ", " + b); } private static void ComputeXYZ(out int r1, out int r2) { r1 = ...; r2 = ...; } }
Microsoft
23
Part 4 •
Statements…
Microsoft
24
Statements in C# •
C# supports the standard assortment…
• • •
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(); if (x > 0 && x < 10) count++; else if (x == -1) ... else { ... while (x > 0) } { ... x--; }
Microsoft
for (int k = 0; k < 10; k++) { ... }
26
foreach •
Specialized foreach loop provided for collections like array – reduces risk of indexing error – provides read only access
int[] data = { 1, 2, 3, 4, 5 }; int sum = 0; foreac h
foreach (int x in data) { sum += x; } typ e
Microsoft
valu e
collectio n
27
Summary •
Standard OOP language support: – namespaces – classes – methods – statements
•
Two types of methods – instance methods require an object to call – static methods are global and thus require only class name
Microsoft
28