Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 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 6 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

19 Nested Types

20 Can class X.Y access its field a ? class X { private int a; public int GetA() { return a; } public class Y : X { public void SetA() { a = 10; } OptionResult ANo – X.Y does not contain field a BNo – X.Y.a is not accessible to X.Y members CYes

21 Can class X.Y access its field a ? class X { private int a; public int GetA() { return a; } public class Y : X { public void SetA() { a = 10; } OptionResult ANo – X.Y does not contain field a BNo – X.Y.a is not accessible to X.Y members CYes private x → x is visible to every member of class X ↓ X.Y is member of X → X.x is visible to class X.Y and X.Y.x is inherited X.x → X.Y.x is visible to X.Y private x → x is visible to every member of class X ↓ X.Y is member of X → X.x is visible to class X.Y and X.Y.x is inherited X.x → X.Y.x is visible to X.Y

22 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

23 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

24 Simple Types (are value types).NET TypeC# Keyword CLS Compliant In-place Size in Bytes (bits) Range System.BytebyteYes1 B (8 b)0 … 255 System.SBytesbyte-1 B (8 b)-128 … 127 System.UInt16ushort-2 B (16 b)0 … 65,535 System.Int16shortYes2 B (16 b)-32,768 … 32,767 System.UInt32uint-4 B (32 b)0 … 4,294,967,295 System.Int32intYes4 B (32 b)-2,147,483,648 … 2,147,483,647 System.UInt64ulong-8 B (64 b)0 … 18,446,744,073,709,551,615 System.Int64longYes8 B (64 b)-9,223,372,036,854,775,808 … 9,223,372,036,854,775,807 System.SinglefloatYes4 B (32 b)IEEE 754: 1-bit sign + 23(+1)-bit mantissa + 8-bit signed exponent: ±3.402823 * 10 38 System.DoubledoubleYes8 B (64 b)IEEE 754: 1-bit sign + 52(+1)-bit mantissa + 11-bit signed exponent: ±1.79769313486232 * 10 308 System.BooleanboolYes-true, false System.DecimaldecimalYes16 B (128 b)1-bit sign + 96-bit integer / 10 0-28, i.e.: ±2 96 / 10 0-28 System.CharcharYes2 B (16 b)UTF-16 characters (Unicode) Compare: in-place size of reference types is platform dependent, e.g. a string variable on a 32-bit platform = 4 B, but on a 64-bit platform = 8 B

25 Simple Types (are value types).NET TypeC# Keyword CLS Compliant In-place Size in Bytes (bits) Range System.BytebyteYes1 B (8 b)0 … 255 System.SBytesbyte-1 B (8 b)-128 … 127 System.UInt16ushort-2 B (16 b)0 … 65,535 System.Int16shortYes2 B (16 b)-32,768 … 32,767 System.UInt32uint-4 B (32 b)0 … 4,294,967,295 System.Int32intYes4 B (32 b)-2,147,483,648 … 2,147,483,647 System.UInt64ulong-8 B (64 b)0 … 18,446,744,073,709,551,615 System.Int64longYes8 B (64 b)-9,223,372,036,854,775,808 … 9,223,372,036,854,775,807 System.SinglefloatYes4 B (32 b)IEEE 754: 1-bit sign + 23(+1)-bit mantissa + 8-bit signed exponent: ±3.402823 * 10 38 System.DoubledoubleYes8 B (64 b)IEEE 754: 1-bit sign + 52(+1)-bit mantissa + 11-bit signed exponent: ±1.79769313486232 * 10 308 System.BooleanboolYes-true, false System.DecimaldecimalYes16 B (128 b)1-bit sign + 96-bit integer / 10 0-28, i.e.: ±2 96 / 10 0-28 System.CharcharYes2 B (16 b)UTF-16 characters (Unicode) 7 decimal digit precision 28 decimal digit precision 15 decimal digit precision

26 Simple Types: Implicit Conversions byte sbyte char short ushort int uint long ulong float double decimal bool All other conversions between types above are possible using an explicit conversion: A B A (A) B e.g.: long a = 1; int b = (int) a; C# vs. C/C++: No conversions possible to or from bool type in C#!

27 Simple Types: Implicit Conversions ≠ Inheritance byte sbyte char short ushort int uint long ulong float double decimal All other conversions between types above are possible using an explicit conversion: A B A (A) B e.g.: long a = 1; int b = (int) a; short is convertible to int ≠ int is inherited from short nor short is inherited from int

28 Simple Types: Literals Integer literals Default type of integer literals is int – larger type is selected if literal’s value does not fit into an int, e.g. 1000000000000long A literal can be implicitly converted to a smaller type if literal’s value fits, e.g.: byte a = 1; Can be decimal, e.g. 254 Can be hexadecimal, e.g. 0xFE A literal case-insensitive suffix forces a subset of possible types: u U uint, ulong l L long, ulong ul uL Ul UL lu lU Lu LU ulong Real literals Default type of real literals is double. Format: [123456789].123456789[E[+/-]123], e.g. 10.0, 10E15, 10E-4,.14159 A literal case-insensitive suffix forces literal type: f Ffloat d Ddouble m Mdecimal

29 Char Literals & Escape Sequences Char literal: a single character or escape sequence in quotes: 'A' or '\n' or '\x5C' Escape SequenceCharacterUnicode Code (in hex) \''27 \""22 \\\5C \0Null0 \bBackspace8 \nNew lineA \rCarriage return D \tTab (horizontal) 9 \xWXYZAny character WXYZ \aAlert7 \fForm feedC \vVertical tabB

30 Note on Decimals.NET TypeC# Keyword CLS Compliant In-place Size in Bytes (bits) Range System.BytebyteYes1 B (8 b)0 … 255 System.SBytesbyte-1 B (8 b)-128 … 127 System.UInt16ushort-2 B (16 b)0 … 65,535 System.Int16shortYes2 B (16 b)-32,768 … 32,767 System.UInt32uint-4 B (32 b)0 … 4,294,967,295 System.Int32intYes4 B (32 b)-2,147,483,648 … 2,147,483,647 System.UInt64ulong-8 B (64 b)0 … 18,446,744,073,709,551,615 System.Int64longYes8 B (64 b)-9,223,372,036,854,775,808 … 9,223,372,036,854,775,807 System.SinglefloatYes4 B (32 b)IEEE 754: 1-bit sign + 23(+1)-bit mantissa + 8-bit signed exponent: ±3.402823 * 10 38 System.DoubledoubleYes8 B (64 b)IEEE 754: 1-bit sign + 52(+1)-bit mantissa + 11-bit signed exponent: ±1.79769313486232 * 10 308 System.BooleanboolYes-true, false System.DecimaldecimalYes16 B (128 b)1-bit sign + 96-bit integer / 10 0-28, i.e.: ±2 96 / 10 0-28 System.CharcharYes2 B (16 b)UTF-16 characters (Unicode) Values in Decimals are not normalized: decimal a = 1; decimal b = 1.0000M; decimal c = 1.00M; decimal d = (decimal) 1.000; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d); Console.WriteLine(a == b); Console.WriteLine(a == c); Console.WriteLine(a == d); double a = 1; double b = 1.0000; double c = 1.00; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c);

31 What is the behavior of the following program? class Program { static void Main(string[] args) { int a = 10; int b = 0; L1:int c = a / b; L2:int d = c + 15; L3:Console.WriteLine(d); } OptionResult AIt will not compile. BIt will generate a runtime error at line L1. CIt will generate a runtime error at line L2. DIt will generate a runtime error at line L3. EIt will print something to the standard output.

32 What is the behavior of the following program? class Program { static void Main(string[] args) { int a = 10; int b = 0; L1:int c = a / b; L2:int d = c + 15; L3:Console.WriteLine(d); } OptionResult AIt will not compile. BIt will generate a runtime error at line L1 – DivideByZeroException. CIt will generate a runtime error at line L2. DIt will generate a runtime error at line L3. EIt will print something to the standard output.

33 What is the behavior of the following program? class Program { static void Main(string[] args) { double a = 10; double b = 0; L1:double c = a / b; L2:double d = c + 15; L3:Console.WriteLine(d); } OptionResult AIt will not compile. BIt will generate a runtime error at line L1. CIt will generate a runtime error at line L2. DIt will generate a runtime error at line L3. EIt will print something to the standard output.

34 What is the behavior of the following program? class Program { static void Main(string[] args) { double a = 10; double b = 0; L1:double c = a / b; L2:double d = c + 15; L3:Console.WriteLine(d); } OptionResult AIt will not compile. BIt will generate a runtime error at line L1. CIt will generate a runtime error at line L2. DIt will generate a runtime error at line L3. EIt will print something to the standard output: “Infinity” or “+nekonečno” in Czech locale (see double.PositiveInfinity, double.NegativeInfinity, double.NaN).

35 What is the behavior of the following program? class Program { static void Main(string[] args) { double a = 10; double b = -0; L1:double c = a / b; L2:double d = c + 15; L3:Console.WriteLine(d); } OptionResult AIt will not compile. BIt will generate a runtime error at line L1. CIt will generate a runtime error at line L2. DIt will generate a runtime error at line L3. EIt will print something to the standard output.

36 What is the behavior of the following program? class Program { static void Main(string[] args) { double a = 10; double b = -0; L1:double c = a / b; L2:double d = c + 15; L3:Console.WriteLine(d); } OptionResult AIt will not compile. BIt will generate a runtime error at line L1. CIt will generate a runtime error at line L2. DIt will generate a runtime error at line L3. EIt will print something to the standard output: “Infinity” or “+nekonečno” in Czech locale (see double.PositiveInfinity, double.NegativeInfinity, double.NaN). -0 is an int value, which gets converted to 0 of int (there is no negative 0 in interger types), then to 0.0 of double.

37 What is the behavior of the following program? class Program { static void Main(string[] args) { double a = 10; double b = -0.0; L1:double c = a / b; L2:double d = c + 15; L3:Console.WriteLine(d); } OptionResult AIt will not compile. BIt will generate a runtime error at line L1. CIt will generate a runtime error at line L2. DIt will generate a runtime error at line L3. EIt will print something to the standard output: “-Infinity” or “-nekonečno” in Czech locale (see double.PositiveInfinity, double.NegativeInfinity, double.NaN). -0.0 is a valid and unique double value, distinct from 0.0.


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

Similar presentations


Ads by Google