[PDF] advanced calculator app for android
[PDF] advanced cisco router configuration pdf
[PDF] advanced complex analysis pdf
[PDF] advanced computational methods in science and engineering pdf
[PDF] advanced concepts in java
[PDF] advanced css book
[PDF] advanced css tutorial with example pdf
[PDF] advanced css3 tutorial pdf free download
[PDF] advanced dance moves ballet
[PDF] advanced db2 sql queries
[PDF] advanced dos commands pdf
[PDF] advanced english class pdf
[PDF] advanced english expressions list
[PDF] advanced english grammar test pdf
[PDF] advanced english learning book pdf
1
Advanced C#
University of Linz, Austria
moessenboeck@ssw.uni-linz.ac.at
Contents•
I nheritance I nterfaces D elegates E xceptions N amespaces and Assemblies A ttribute s T hreads X ML
Comments
2
Inheritance
3
Syntax
class A { // base cl ass int a;public A() {...}pu b l ic vo id
F() {...}
}class B : A // subclass (inherits from A , extends A int b;public B() {...}publi c voi d G B inherits a and F (), it adds b and G c onstructors are not inherited i nherited methods can be overridden (see later) S ingle inheritance : a class can only inherit from one base class, but it can implement multiple interfaces. A class can only inherit from a class , not from a struct. S tructs cannot inherit from another type, but they can implement multiple interfaces. A class without explicit base class inherits from object 4
Asignments and Type Checks
class A { class B : A {...}class C: B {...
Assignments
A a = new A();
st atic t y pe of a : the type specified in the declaration (here A dynamic type of a : the type of the objec t 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 be cause it makes no sense a = null;if ( a is C // false: if a null a is T always returns false 5
Checked Type CastsCast
A a = new C();B b =
(B) a // if ( a is B ) stat.type( a ) is B in this expression ; else excepti o n 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 // i f (a is B) b = (B)a; el se b = null; C c = a as C a = null;c = a as C // c == null 6 Overriding of MethodsOnly methods that are declared as virtual can be overridden in subclasses class A { public void F() {...} // cannot be overridden publi c virtual void G // can be overridden in a subclass
Overriding methods must be declared as
override class B : A { public void F() {...} // w arning: hides inherited F() use new public void G() {...} // warn ing: hides inherited G() use new publi c override void G() { // ok: ov errides inherited G base.G() // calls inherited G() M ethod s i gnatures must be identical s ame number and types of parameters (including function type) -s a m e visibility (public, protected, ...). P roperties and indexers can also be overridden (virtual, override). S tatic methods cannot be overridden. 7
Dynamic Binding (simplified)
class A { publi c virtual voi d
WhoAreYou() { Consol
e.Wr iteLine("I am an A" }class B : A { publi c override voi d
WhoAreYou() {
Consol
e.
WriteLi
ne("I am a B"); }
A message invok
e s the method belonging to the dynamic ty pe 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 voi d
Use (A x) {
x.WhoAreYou(); }Use(new A()); // "I am an A"
Use(new B());
// "I am a B" 8
HidingMembers can be declared as
new in a subclass. They hide inherited members with the same name. class A { pu b l ic in t x pu b l ic vo id
F() {...}
public virtual void G() {...} }class B : A { publi c new int x; publi c new void F() {...} publi c 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(); // call s A.F and A.G ! 9
Dynamic Binding (with hiding)class A {
publi c virtual voi d
WhoAreYou() { Consol
e.Wr iteLine("I am an A" }class B : A { publi c override voi d
WhoAreYou() {
Consol
e.
WriteLi
ne("I am a B"); } }class C : B { publi c new virtual void WhoAreYou() { Console.
WriteLi
ne("I am a C"); } }class D : C { publi c override voi d
WhoAreYou() {
Consol
e.
WriteLi
ne("I am a D"); } }C c = new D();c.WhoAreYou(); // "I am a D"
A a = new D();a.WhoAreYou();
// "I am a B" !! 10
Fragile Base Class ProblemInitial situation
class LibraryClass { public void CleanUp() { ... } }class M y
Class :
Li braryClass { public void Delete() { ... erase the hard disk
Later: vendor ships new version of
LibraryClass
class LibraryClass { string name;public virtual void Dele te() { name = null; } public void CleanUp() { Delete(); ... } I n Java the call myObj.CleanUp() would erase the hard disk! I n 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 11
Constructors and Inheritance
Implicit call of the base class constructor
Explicit call
class A { }class B : A { pu bquotesdbs_dbs14.pdfusesText_20