Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek

Similar presentations


Presentation on theme: "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek"— Presentation transcript:

1 CHARLES UNIVERSITY IN PRAGUE http://d3s.mff.cuni.cz/~jezek faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek pavel.jezek@d3s.mff.cuni.cz 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 CLI Type System All types Reference types (allocated on managed heap) PointersValue types (allocated in-place [with exceptions] ) Classes (e.g. strings) Interfaces ArraysDelegates Simple types (Int32, Int64, Double, Boolean, Char, …) Nullables Enumerations Structures User defined structures

3 How Would You Implement List.Clear() ? OptionComplexity with Respect to Number of Elements in List (n) AO(1) BO(n) CSomething else.

4 How Would You Implement List.Clear() ? OptionComplexity with Respect to Number of Elements in List (n) AO(1) BO(n) – set all elements to null ! Mind the GC! CSomething else.

5 What about String.Substring?

6 CLI Type System All types Reference types (allocated on managed heap) PointersValue types (allocated in-place [with exceptions] ) Classes (e.g. strings) Interfaces ArraysDelegates Simple types (Int32, Int64, Double, Boolean, Char, …) Nullables Enumerations Structures User defined structures

7 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? )

8 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? ) Interfaces are not inherited from System.Object, but System.Object members are callable/accessible via any interface type expression.

9 Example: Inheritance of private members

10 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? )

11 CLI Type Inheritance (Sealed Types) 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? ) sealed Optionally sealed

12 Ref. Type DOES NOT Imply Instances 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? )

13 Hiding Members can be declared as new in a subclass. They hide inherited members with the same name and signature. 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!

14 What is the output the following program? class A { public string className = “A”; } class B : A { private string className = “B”; } class Program { static void Main(string[] args) { Console.WriteLine(new B().className); } OptionResult AIt will not compile – error in class B. BIt will not compile – error in class Program. CA DB EIt will generate a runtime error.

15 What is the output the following program? class A { public string className = “A”; } class B : A { private string className = “B”; } class Program { static void Main(string[] args) { Console.WriteLine(new B().className); } OptionResult AIt will not compile – error in class B. BIt will not compile – error in class Program. CA DB EIt will generate a runtime error. a compiler warning: use new keyword new

16 What is the output the following program? class A { public string className = “A”; } class B : A { private string className = “B”; } class Program { static void Main(string[] args) { Console.WriteLine(new B().className); } OptionResult AIt will not compile – error in class B. BIt will not compile – error in class Program. CA & a compiler warning: use new keyword. DB EIt will generate a runtime error.

17 What is the output the following program? class A { public string className = “A”; } class B : A { private string className = “B”; public void f() { Console.WriteLine(className); } class Program { static void Main(string[] args) { Console.Write(new B().className); new B().f(); } OptionResult AA BA B CB A DB

18 What is the output the following program? class A { public string className = “A”; } class B : A { private string className = “B”; public void f() { Console.WriteLine(className); } class Program { static void Main(string[] args) { Console.Write(new B().className); new B().f(); } OptionResult AA BA B & a compiler warning: use new keyword. CB A DB


Download ppt "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek"

Similar presentations


Ads by Google