Docsity

Advanced C#, Exams of Advanced Computer Programming

Partial preview of the text

Download Advanced C# and more Exams Advanced Computer Programming in PDF only on Docsity!

Advanced C#

H.Mössenböck

University of Linz, Austria

[email protected]

Contents • Inheritance • Interfaces • Delegates • Exceptions • Namespaces and Assemblies • Attributes • Threads • XML Comments

Inheritance

Asignments and Type Checks

class A {.

class B : A {...} class C: B {...}

Assignments

A a = new A();

// static type of

a

: the type specified in the declaration (here

A

// dynamic type of

a

: the type of the object in

a

(here also

A

a = new B();

// dynamic type of

a

is

B

a = new C();

// dynamic type of

a

is

C

B b = a;

// forbidden; compilation error

Run time type checks

a = new C(); if (a is C) ...

// true, if dynamic type of

a

is

C

or a subclass; otherwise false

if (a is B) ...

// true

if (a is A) ...

// true, but warning because it makes no sense

a = null; if (a is C) ...

// false: if

a

== null,

a is T

always returns false

Checked Type Casts Cast

A a = new C(); B b = (B) a;

// if (

a^

is

B

) stat.type(

a ) is

B

in this expression; else exception

C c = (C) a; a = null; c = (C) a;

// ok

Î

null can be casted to any reference type

as

A a = new C(); B b = a as B;

// if (a is B) b = (B)a; else b = null;

C c = a as C; a = null; c = a as C;

// c == null

Dynamic Binding (simplified)

class A {

public virtual void WhoAreYou() { Console.WriteLine("I am an A"); } } class B : A {

public override void WhoAreYou() { Console.WriteLine("I am a B"); } }

A message invokes the method belonging to the dynamic type of the receiver (not quite true, see later)

A a = new B();a.WhoAreYou();

// "I am a B"

Every method that can work with

A

can also work with

B

void Use (A x) {

x.WhoAreYou(); } Use(new A());

// "I am an A"

Use(new B());

// "I am a B"

Hiding Members can be declared as new in a subclass.They

hide

inherited members with the same name.

class A {

public int x;public void F() {...}public virtual void G() {...} } class B : A {

public new int x;public new void F() {...}public new void G() {...} } B b = new B();b.x = ...;

// accesses B.x

b.F(); ... b.G();

// calls B.F and B.G

((A)b).x = ...;

// accesses A.x!

((A)b).F(); ... ((A)b).G();

// calls A.F and A.G!

Fragile Base Class Problem Initial situation

class LibraryClass {

public void CleanUp() { ... } } class MyClass : LibraryClass {

public void Delete() { ...

erase the hard disk

Later: vendor ships new version of

LibraryClass

class LibraryClass {

string name;public virtual void Delete() { name = null; }public void CleanUp() { Delete(); ... } }

-^

In Java the call

myObj.CleanUp()

would erase the hard disk!

-^

In C# nothing happens, as long as

MyClass

is not recompiled.

MyClass

still relies on the old version of

LibraryClass (

Versioning)

Î

old

CleanUp()

does not call

LibraryClass.Delete()

-^

If

MyClass

is recompiled, the compiler forces

Delete

to be declared as

new

or

override

Constructors and Inheritance

Implicit call of the base class constructor

Explicit call

class A {

} class B : A {

public B(int x) {...} }

class A {

public A() {...} } class B : A {

public B(int x) {...} }

class A {

public A(int x) {...} } class B : A {

public B(int x) {...} }

class A {

public A(int x) {...} } class B : A {

public B(int x) : base(x) {...} }

B b = new B(3);

B b = new B(3);

B b = new B(3);

B b = new B(3);

OK -^

default constr. A()

-^

B(int x)

OK -^

A()

-^

B(int x)

Error! -^

no explicit call of the A() constructor

-^

default constr. A() does not exist

OK -^

A(int x)

-^

B(int x)

Abstract Classes Example

abstract class Stream {

public abstract void Write(char ch);public void WriteString(string s) { foreach (char ch in s) Write(s); } } class File : Stream {

public override void Write(char ch) {...

write ch to disk

Note•^

Abstract methods do not have an implementation.

-^

Abstract methods are implicitly

virtual

-^

If a class has abstract methods it must be declared

abstract

itself.

-^

One cannot create objects of an abstract class.

Abstract Properties and Indexers Example

abstract class Sequence {

public abstract void Add(object x);

// method

public abstract string Name { get; }

// property

public abstract object this [int i] { get; set; }

// indexer

} class List : Sequence {

public override void Add(object x) {...}public override string Name { get {...} }public override object this [int i] { get {...} set {...} } }

Note•^

Overridden indexers and properties must have the same get and set methods as in thebase class

Interfaces

Syntax

public interface IList : ICollection, IEnumerable {

int Add (object value);

// methods

bool Contains (object value);...bool IsReadOnly { get; }

// property

...object this [int index] { get; set; }

// indexer

-^

Interface = purely abstract class; only signatures, no implementation.

-^

May contain methods, properties, indexers and events(no fields, constants, constructors, destructors, operators, nested types).

-^

Interface members are implicitly

public abstract

virtual

-^

Interface members must not be

static

-^

Classes and structs may implement multiple interfaces.

-^

Interfaces can extend other interfaces.

Working with Interfaces Assignments:

MyClass c = new MyClass(); IList list = c;

Method calls:

list.Add("Tom");

// dynamic binding => MyClass.Add

Type checks:

if (list is MyClass) ...

// true

Type casts:

c = list as MyClass; c = (MyClass) list; ISerializable ser = (ISerializable) list;

MyClass

MyBaseClass

<>

IList

<>ISerializable

Example interface ISimpleReader {

int Read(); } interface IReader : ISimpleReader {

void Open(string name); void Close(); } class Terminal : ISimpleReader {

public int Read() { ... } } class File : IReader {

public int Read() { ... } public void Open(string name) { ... } public void Close() { ... } } ISimpleReader sr = null;

// null can be assigned to any interface variable

sr = new Terminal(); sr = new File(); IReader r = new File(); sr = r;

Terminal Read

<> ISimpleReader Read

File ReadOpenClose

<> IReader Open Close