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 faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek 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 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? )

4 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.

5 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

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

7 Visibility Visibility modifiers: publicAccess is not restricted. protectedAccess is limited to the containing class or types derived from the containing class. internalAccess is limited to the current assembly. protected internalAccess is limited to the current assembly or types derived from the containing class. privateAccess is limited to the containing type. Default visibility in: enumpublic classprivate interfacepublic structprivate

8 Nested Types

9 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

10 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

11 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

12 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

13 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: ± * System.DoubledoubleYes8 B (64 b)IEEE 754: 1-bit sign + 52(+1)-bit mantissa + 11-bit signed exponent: ± * System.BooleanboolYes-true, false System.DecimaldecimalYes16 B (128 b)1-bit sign + 96-bit integer / , i.e.: ±2 96 / 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

14 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: ± * System.DoubledoubleYes8 B (64 b)IEEE 754: 1-bit sign + 52(+1)-bit mantissa + 11-bit signed exponent: ± * System.BooleanboolYes-true, false System.DecimaldecimalYes16 B (128 b)1-bit sign + 96-bit integer / , i.e.: ±2 96 / System.CharcharYes2 B (16 b)UTF-16 characters (Unicode) 7 decimal digit precision 28 decimal digit precision 15 decimal digit precision

15 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#!

16 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

17 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 long 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: [ ] [E[+/-]123], e.g. 10.0, 10E15, 10E-4, A literal case-insensitive suffix forces literal type: f Ffloat d Ddouble m Mdecimal

18 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

19 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: ± * System.DoubledoubleYes8 B (64 b)IEEE 754: 1-bit sign + 52(+1)-bit mantissa + 11-bit signed exponent: ± * System.BooleanboolYes-true, false System.DecimaldecimalYes16 B (128 b)1-bit sign + 96-bit integer / , i.e.: ±2 96 / System.CharcharYes2 B (16 b)UTF-16 characters (Unicode) Values in Decimals are not normalized: decimal a = 1; decimal b = M; 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 = ; double c = 1.00; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c);

20 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.

21 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.

22 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.

23 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).

24 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.

25 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.

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