Download presentation
Presentation is loading. Please wait.
Published byBrice Wiggins Modified over 9 years ago
1
CHARLES UNIVERSITY IN PRAGUE http://d3s.mff.cuni.cz/~jezek faculty of mathematics and physics C# Language &.NET Platform 8 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 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? )
3
Class System.Object Topmost base class of all other classes class Object { protected object MemberwiseClone() {...} public Type GetType() {...} public virtual bool Equals (object o) {...} public virtual string ToString() {...} public virtual int GetHashCode() {...} public static bool ReferenceEquals(object objA, object objB); }
4
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? )
5
ValueType.Equals Override
6
Namespaces & Nested Types
7
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
8
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
9
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
10
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
11
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
12
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
13
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#!
14
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
15
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
16
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
17
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);
18
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.
19
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.
20
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.
21
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).
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). -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.
24
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.