Presentation is loading. Please wait.

Presentation is loading. Please wait.

- This slide is intentionally left blank - Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System.

Similar presentations


Presentation on theme: "- This slide is intentionally left blank - Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System."— Presentation transcript:

1 - This slide is intentionally left blank - Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License (http://www.msdnaa.net/curriculum/license_curriculum.aspx)

2

3 FUN WITH C# EPISODE XII THE VIRTUAL METHODS

4 Generic Class Buffer class Buffer { private Element[] data; public Buffer(int size) {...} public void Put(Element x) {...} public Element Get() {...} } placeholder typegeneric type works also for structs and interfaces placeholder type Element can be used like a normal type Usage Buffer a = new Buffer (100); a.Put(3);// accepts only int parameters; no boxing int i = a.Get();// no type cast needed! Buffer b = new Buffer (100); b.Put(new Rectangle());// accepts only Rectangle parameters Rectangle r = b.Get();// no typ cast needed! Benefits homogeneous data structure with compile-time type checking efficient (no boxing, no type casts)

5 CLI Type Inheritance System.Object (C# keyword: object ) user-defined classes (C# keyword: class ) delegates (C# keyword: delegate ) pointers (C#: Type * ) System.Delegate System.MulticastDelegate System.ValueType System.Enum System.Array arrays (C#: Type[] or Type[,] ) System.String (C# keyword: string ) interfaces (C# keyword: interface ) user-defined structures (C# keyword: struct ) enumerations (C# keyword: enum ) System.Int32 (C# keyword: int ) System.Int64 (C# keyword: long ) System.Double (C# keyword: double ) System.Boolean (C# keyword: bool ) … simple types System.Nullable (C#: Type? )

6 Arrays One-dimensional arrays int[] a = new int[3]; int[] b = new int[] {3, 4, 5}; int[] c = {3, 4, 5}; SomeClass[] d = new SomeClass[10];// array of references SomeStruct[] e = new SomeStruct[10];// array of values (directly in the array) Multidimensional arrays (rectangular) int[,] a = new int[2, 3];// block matrix int[,] b = {{1, 2, 3}, {4, 5, 6}};// can be initialized directly int[,,] c = new int[2, 4, 2]; Multidimensional arrays (jagged) int[][] a = new int[2][]; // array of references to other arrays a[0] = new int[] {1, 2, 3}; // cannot be initialized directly a[1] = new int[] {4, 5, 6};

7 Multidimensional Arrays int[][] a = new int[2][]; a[0] = new int[3]; a[1] = new int[4]; int x = a[0][1]; Jagged (like in Java) a a[0] a[1] int[,] a = new int[2, 3]; int x = a[0, 1]; Rectangular (like in C/C++) a a[0][1] a[0, 1]

8 Multidimensional Arrays int[][] a = new int[2][]; a[0] = new int[3]; a[1] = new int[4]; int x = a[0][1]; Jagged (like in Java) a a[0] a[1] int[,] a = new int[2, 3]; int x = a[0, 1]; Rectangular (like in C/C++) a a[0][1] a[0, 1] Antipattern in current.NET (slower access)

9 Other Array Properties Indexes start at 0 Array length int[] a = new int[3]; Console.WriteLine(a.Length); // 3 int[][] b = new int[3][]; b[0] = new int[4]; Console.WriteLine("{0}, {1}", b.Length, b[0].Length); // 3, 4 int[,] c = new int[3, 4]; Console.WriteLine(c.Length); // 12 Console.WriteLine("{0}, {1}", c.GetLength(0), c.GetLength(1)); // 3, 4 System.Array provides some useful array operations int[] a = {7, 2, 5}; int[] b = new int[2]; Array.Copy(a, b, 2);// copies a[0..1] to b Array.Sort(b);...

10 Default constructor

11 Constructors and Inheritance class A {... } class B : A { public B(int x) {...} } class A {... } class B : A { public B(int x) {...} } B b = new B(3); OK -Default-constr. A() -B(int x) OK -Default-constr. A() -B(int x) class A { public A() {...} } class B : A { public B(int x) {...} } class A { public A() {...} } class B : A { public B(int x) {...} } B b = new B(3); OK -A() -B(int x) OK -A() -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) {...} } B b = new B(3); Error! -no explicit call of the A() constructor -default constr. A() does not exist Error! -no explicit call of the A() constructor -default constr. A() does not exist class A { public A(int x) {...} } class B : A { public B(int x) : base(x) {...} } class A { public A(int x) {...} } class B : A { public B(int x) : base(x) {...} } B b = new B(3); OK -A(int x) -B(int x) OK -A(int x) -B(int x) Implicit call of the base class constructorExplicit call class A { public A() {...} } class B : A { … } class A { public A() {...} } class B : A { … } B b = new B(); OK -A() -Default- constr. B() OK -A() -Default- constr. B()

12 Virtual Methods

13 Virtual Methods: A More Complex Example class Animal { public virtual void WhoAreYou() { Console.WriteLine("I am an animal"); } } class Mammal : Animal { public override void WhoAreYou() { Console.WriteLine("I am a mammal"); } } class Dog : Mammal { public new virtual void WhoAreYou() { Console.WriteLine("I am a dog"); } } class Beagle : Dog { public override void WhoAreYou() { Console.WriteLine("I am a beagle"); } } Dog pet = new Beagle(); pet.WhoAreYou(); Animal pet = new Beagle(); pet.WhoAreYou();

14 Virtual Methods: A More Complex Example class Animal { public virtual void WhoAreYou() { Console.WriteLine("I am an animal"); } } class Mammal : Animal { public override void WhoAreYou() { Console.WriteLine("I am a mammal"); } } class Dog : Mammal { public new virtual void WhoAreYou() { Console.WriteLine("I am a dog"); } } class Beagle : Dog { public override void WhoAreYou() { Console.WriteLine("I am a beagle"); } } Dog pet = new Beagle(); pet.WhoAreYou();// "I am a beagle" Animal pet = new Beagle(); pet.WhoAreYou();// "I am a mammal"

15 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 (declared or inherited) it must be abstract itself. One cannot create objects of an abstract class..

16 Virtual and Non-virtual Methods Method TypeC++JavaC# virtual (new VMT entry) virtual nothing virtual virtual (reuse existing VMT entry) virtual nothing (optional @override annotation) override non-virtualnothingnot supported/ final (is similar to non- virtual in some scenarios) nothing Java final = C# sealed


Download ppt "- This slide is intentionally left blank - Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System."

Similar presentations


Ads by Google