Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

6 Enums

7 CLI Type Inheritance (Nullable 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? ) Can be assigned a null value

8 Nullable Types int i = 123; int? x; x = i; x = 456; x = null; if (x != null) { int j; j = x; // ERROR j = (int) x; // OK } else { int j = (int) x; // EXCEPTION – InvalidOperationException }

9 Nullable Types [Serializable] public struct Nullable where T : struct { public Nullable ( T value ) public bool HasValue { get; } public T Value { get; } public T GetValueOrDefault () public T GetValueOrDefault ( T defaultValue ) … } int? x = 456; int? y = null; x == null null == x // !x.HasValue x != null null != x // x.HasValue int? u = x + y; // (x.HasValue && y.HasValue) ? (x + y) : null

10 Nullable Types [Serializable] public struct Nullable where T : struct { public Nullable ( T value ) public bool HasValue { get; } public T Value { get; } public T GetValueOrDefault () public T GetValueOrDefault ( T defaultValue ) … } int? x = 456; int? y = null; x == null null == x // !x.HasValue x != null null != x // x.HasValue int? u = x + y; // (x.HasValue && y.HasValue) ? (x + y) : null int? z = x ?? y; // x.HasValue ? x : y ?? operator can be used with all “nullable” types, i.e. reference types as well

11 Nullable Types, bool?

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

13 Crossing Value/Reference Type Boundary 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? ) un/boxing

14 Nullable Types – Boxing and Unboxing [Serializable] public struct Nullable where T : struct { public Nullable ( T value ) public bool HasValue { get; } public T Value { get; } public T GetValueOrDefault () public T GetValueOrDefault ( T defaultValue ) … } int i = 123; int? x = 456; int? y = null; object o1 = i; // o1 = reference to boxed int 123 object o2 = x; // o2 = reference to boxed int 456 object o3 = y; // o3 = null int i1 = (int)o1; // i1 = 123 int i2 = (int)o2; // i2 = 456 int i3 = (int)o3; // Error, System.NullReferenceException int? ni1 = (int?)o1; // ni1 = 123 int? ni2 = (int?)o2; // ni2 = 456 int? ni3 = (int?)o3; // ni3 = null

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

16 Operator Overloading Static method for implementing a certain operator struct Fraction { int x, y; public Fraction (int x, int y) {this.x = x; this.y = y; } public static Fraction operator + (Fraction a, Fraction b) { return new Fraction(a.x * b.y + b.x * a.y, a.y * b.y); } Usage Fraction a = new Fraction(1, 2); Fraction b = new Fraction(3, 4); Fraction c = a + b; // c.x == 10, c.y == 8 The following operators can be overloaded: arithmetic: +, - (unary and binary), *, /, %, ++, -- relational: ==, !=,, = bit operators: &, |, ^ others: !, ~, >>, <<, true, false Must always return a function result If == ( =, >, false) must be overloaded as well.

17 Overloading of && and || In order to overload && and ||, one must overload &, |, true and false class TriState { int state; // -1 == false, +1 == true, 0 == undecided public TriState(int s) { state = s; } public static bool operator true (TriState x) { return x.state > 0; } public static bool operator false (TriState x) { return x.state < 0; } public static TriState operator & (TriState x, TriState y) { if (x.state > 0 && y.state > 0) return new TriState(1); else if (x.state < 0 || y.state < 0) return new TriState(-1); else return new TriState(0); } public static TriState operator | (TriState x, TriState y) { if (x.state > 0 || y.state > 0) return new TriState(1); else if (x.state < 0 && y.state < 0) return new TriState(-1); else return new TriState(0); } true and false are called implicitly TriState x, y; if (x)... => if (TriState.true(x))... x = x && y; => x = TriState.false(x) ? x : TriState.&(x, y); x = x || y; => x = TriState.true(x) ? x : TriState.|(x, y)

18 Conversion Operators Implicit conversion -If the conversion is always possible without loss of precision -e.g. long = int; Explicit conversion -If a run time check is necessary or truncation is possible -e.g. int = (int) long; Conversion operators for user-defined types class Fraction { int x, y;... public static implicit operator Fraction (int x) { return new Fraction(x, 1); } public static explicit operator int (Fraction f) { return f.x / f.y; } } Usage Fraction f = 3;// implicit conversion, f.x == 3, f.y == 1 int i = (int) f;// explicit conversion, i == 3

19 Programmable operator for indexing a collection class File { FileStream s; public int this [int index] { get {s.Seek(index, SeekOrigin.Begin); return s.ReadByte(); } set {s.Seek(index, SeekOrigin.Begin); s.WriteByte((byte) value); } Usage File f =...; int x = f[10];// calls f.get(10) f[10] = 'A';// calls f.set(10, 'A') get or set method can be omitted (write-only / read-only) indexers can be overloaded with different index type.NET library has indexers for string (s[i]), List (a[i]), etc. Indexers (parametric properties)

20 Indexers (other example 1) class MonthlySales { int[] apples = new int[12]; int[] bananas = new int[12];... public int this[int month] {// set method omitted => read-only get { return apples[month-1] + bananas[month-1]; } } public int this[string month] { // overloaded read-only indexer get { switch (month) { case "Jan": return apples[0] + bananas[0]; case "Feb": return apples[1] + bananas[1];... } MonthlySales sales = new MonthlySales();... Console.WriteLine(sales[1] + sales["Feb"]);

21 Indexers (other example 2) class MonthlySales { int[] apples = new int[12]; int[] bananas = new int[12];... public int this[int month] {// set method omitted => read-only get { return apples[month-1] + bananas[month-1]; } } public int this[int month, string kind] { // overloaded read-only indexer get { switch (kind) { case "apple": return apples[month-1]; case "banana": return bananas[month-1];... } MonthlySales sales = new MonthlySales();... Console.WriteLine(sales[1]); Console.WriteLine(sales[1, "banana"]); Console.WriteLine(sales[1, "apple"]);

22 Special Operator Methods Operators, indexers, properties and events are compiled into “normal” methods - get_* (property getter) - set_* (property setter) - get_Item (indexer getter) - set_Item (indexer setter) - add_* (event handler addition) - remove_* (event handler removal) - op_Addition (binary +) - op_Subtraction (binary -) - op_Implicit (implicit cast) - op_Explicit (explicit cast) etc.


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

Similar presentations


Ads by Google