02 Csharp PDF
02 Csharp PDF
Hanspeter Mössenböck
Features of C#
2
New Features in C#
3
Type System
Uniform Type System
types
5
Multi-dimensional Arrays
Jagged (like in Java) a
a[0][1]
int x = a[0][1];
6
Boxing and Unboxing
Even value types (int, struct, enum) are compatible with object!
Boxing
In the assignment
object obj = 3;
the value 3 is wrapped up in a heap object
obj class TempInt {
int val;
3 TempInt(int x) {val = x;}
}
Unboxing
In the assignment
int x = (int) obj;
the int value is unwrapped again
7
Boxing/Unboxing
Allows generic container types
class Queue {
...
public void Enqueue(object x) {...}
public object Dequeue() {...}
...
}
This Queue can be used with reference types and value types
Queue q = new Queue();
q.Enqueue(new Rectangle());
q.Enqueue(3);
8
Object-orientation
Classes and Inheritance
C# Java
class A { class A {
private int x; private int x;
public A(int x) { this.x = x; } public A(int x) { this.x = x; }
} }
10
Overriding Methods
C# Java
class A { class A {
... ...
public virtual void Foo() { ...} public void Foo() { ...}
} }
11
Method Parameters
value parameters (input parameters) call by value
void Inc(int x) {x = x + 1;}
void Foo() {
int val = 3;
Inc(val); // val == 3
}
out parameters (output parameters) like a ref parameter, but used for returning
void Read (out int first, out int next) { values from a method.
first = Console.Read(); next = Console.Read();
}
void Foo() {
int first, next;
Read(out first, out next);
}
12
Properties
Syntactic abbreviation for get/set methods
class Data { property type
property name
FileStream s;
x = account.Balance; // ok
account.Balance = ...; // forbidden
Why properties?
• they allow read-only and write-only data
• they allow plausibility checks for field accesses
• the interface and the implementation of the data can differ
14
Indexers
User-defined operations for indexing a collection
Usage
File f = ...;
int x = f[10]; // calls f.get(10)
f[10] = 'A'; // calls f.set(10, 'A')
15
Indexers (another example)
class MonthlySales {
int[] apples = new int[12];
int[] bananas = new int[12];
...
public int this[int i] { // set operation missing => read-only
get { return apples[i-1] + bananas[i-1]; }
}
16
Dynamic Binding (with Hiding)
class Animal {
public virtual void WhoAreYou() { Console.WriteLine("I am an animal"); }
}
17
Delegates
Delegate = Method Type
Declaration of a delegate type
delegate void Notifier (string sender); // normal method signature
// with the keyword delegate
19
Assignment of different methods
Any compatible method can be assigned to a delegate variable
void SayGoodBye(string sender) {
Console.WriteLine("Good bye from " + sender);
}
20
Creation of delegate values
new DelegateType (obj.Method)
21
Multicast Delegates
A delegate variable can store multiple methods!
Notifier notify;
notify = new Notifier(SayHello);
notify += new Notifier(SayGoodBye);
22
Threads
Threads
C# Java
void P() { class MyThread extends Thread {
... thread actions ... public void run() {
} ... thread actions ...
}
Thread t = new Thread(new ThreadStart(P)); }
t.Start();
Thread t = new MyThread();
t.start();
24
Thread Synchronisation
C# Java
class Buffer { class Buffer {
int[] buf = new int[10]; int[] buf = new int[10];
int head = 0, tail = 0, n = 0; int head = 0, tail = 0; n = 0;
Example
[Serializable]
class C {...} // makes C serializable
[Serializable] [Obsolete]
class C {...}
[Serializable, Obsolete]
class C {...}
27
Example: Conditional Attribute
Conditional invocation of methods
class C {
29
Summary
C# is often more convenient than Java
Java C#
String s = ..; String s = ...;
... ...
for (int i = 0; i < s.length(); i++) foreach (char ch in s)
process(s.charAt(i)); process(ch);
31
C# versus Java
Equivalent features
• object-orientation
• exception handling
• threading
• reflection
Additional features
• compatible with other .NET languages (but predominantly under Windows)
• uniform type system (boxing, unboxing)
• block matrixes
• call by reference parameters
• properties, indexers
• delegates
• attributes
• versioning
32