Transcription of Advanced C#
1 1 Advanced C# ssenb ckUniversity of Linz, Inheritance Interfaces Delegates Exceptions Namespaces and Assemblies Attributes Threads XML Comments2 Inheritance3 Syntaxclass A {// base classint a;public A() {..}public void F() {..}}class B: A{// subclass (inherits from A, extends A)int b;public B() {..}public void G() {..}} B inherits aand F(), it adds band G()-constructors are not inherited-inherited methods can be overridden (see later) Single inheritance: a class can only inherit from one base class, but it can implement multiple interfaces.
2 A class can only inherit from a class, not from a struct. Structs cannot inherit from another type, but they can implementmultiple interfaces. A class without explicit base class inherits from and Type Checksclass A {..}class B : A {..}class C: B {..}AssignmentsA a = new A();// static typeof a: the type specified in the declaration (here A)// dynamic typeof a: the type of the object in a(here also A)a = new B();// dynamic type of ais Ba = new C();// dynamic type of ais CB b = a;// forbidden; compilation errorRun time type checksa = new C();if (a is C).
3 // true, if dynamic type of ais Cor a subclass; otherwise falseif (a is B) ..// trueif (a is A) ..// true, but warning because it makes no sensea = null;if (a is C) ..// false: if a== null, a is Talways returns false5 Checked Type CastsCastA a = new C();B b = (B) a;// if (ais B) (a) is Bin this expression; else exceptionC c = (C) a;a = null;c = (C) a;// ok null can be casted to any reference typeasA 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 == null6 Overriding of MethodsOnly methods that are declared as virtualcan be overridden in subclassesclass A {public void F() {.}}
4 }// cannot be overriddenpublic virtualvoid G() {..}// can be overridden in a subclass}Overriding methods must be declared as overrideclass B : A {public void F() {..}// warning: hides inherited F() usenewpublicvoid G() {..}// warning: hides inherited G() usenewpublic overridevoid G() {// ok: overrides inherited ();// calls inherited G()}} Method signatures must be identical-same number and types of parameters (including function type)-samevisibility (public, protected, ..). Properties and indexers can also be overridden (virtual, override).
5 Static methods cannot be Binding (simplified)class A {public virtualvoid WhoAreYou() { ("I am an A"); }}class B : A {public overridevoid WhoAreYou() { ("I am a B"); }}A message invokes the method belonging to the dynamic typeof the receiver(not quite true, see later)A a = new B(); ();// "I am a B"Every method that can work with Acan also work with Bvoid Use (A x) { ();}Use(new A());// "I am an A"Use(new B());// "I am a B"8 HidingMembers can be declared as newin a hideinherited members with the same A {public int x;public void F() {.}}
6 }public virtual void G() {..}}class B : A {public newint x;public newvoid F() {..}public newvoid G() {..}}B b = new B(); = ..;// accesses (); .. ();// calls and ((A)b).x = ..;// accesses !((A)b).F(); .. ((A)b).G(); // calls and !9 Dynamic Binding (with hiding)class A {public virtualvoid WhoAreYou() { ("I am an A"); }}class B : A {public overridevoid WhoAreYou() { ("I am a B"); }}class C : B {public new virtualvoid WhoAreYou() { ("I am a C"); }}class D : C {public overridevoid WhoAreYou() { ("I am a D"); }}C c = new D(); ();// "I am a D"A a = new D(); ();// "I am a B" !
7 !10 Fragile Base Class ProblemInitial situationclass LibraryClass {public void CleanUp() { .. }}class MyClass : LibraryClass {public void Delete() { .. erase the hard }}Later: vendor ships new version of LibraryClassclass LibraryClass {string name;public virtual void Delete() { name = null; }public void CleanUp() { Delete(); .. }} In Java the call ()would erase the hard disk! In C# nothing happens, as long as MyClassis not relies on the old version of LibraryClass (Versioning) old CleanUp()does not call ().
8 If MyClassis recompiled, the compiler forces Deleteto be declared as newor and InheritanceImplicit call of the base class constructorExplicit callclass 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 ofthe A() constructor-default constr.
9 A()does not existOK-A(int x)-B(int x)12 Visibility protectedand internalprotectedVisible in declaring class and its subclasses(more restricive than in Java)internalVisible in declaring assembly (see later)protected internalVisible in declaring class, its subclasses and the declaring assemblyExampleclass Stack{protected int[] values = new int[32];protected int top = -1;public void Push(int x) {..}public int Pop() {..}}class BetterStack: Stack {public bool Contains(int x) {foreach (int y in values) if (x == y) return true;return false;} }class Client {Stack s = new Stack().}
10 [0]..// compilation error!}13 Abstract ClassesExampleabstractclass Stream {public abstract void Write(char ch);public void WriteString(string s) { foreach (char ch in s) Write(s); }}class File : Stream {public overridevoid Write(char ch) {.. write ch to }}Note Abstract methods do not have an implementation. Abstract methods are implicitly virtual. If a class has abstract methods it must be declared abstractitself. One cannot create objects of an abstract Properties and IndexersExampleabstractclass Sequence {public abstractvoid Add(object x);// methodpublic abstractstring Name { get;}// propertypublic abstractobject this [int i] { get; set;}// indexer}class List : Sequence {public overridevoid Add(object x) {.}