Download presentation
Presentation is loading. Please wait.
Published byValentine Bell 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 7 th & 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
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
4
Enums
5
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? )
6
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
7
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 }
8
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
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 int? z = x ?? y; // x.HasValue ? x : y ?? operator can be used with all “nullable” types, i.e. reference types as well
10
Nullable Types, bool?
11
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? )
12
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
13
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
14
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
15
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); }
16
Operator is vs. operator == on Type interface I { } class A { } class B : A { } class C : B, I { } class Program { static void Main(string[] args) { A x = new C(); Console.WriteLine(x is B); Console.WriteLine(x is C); Console.WriteLine(x is I); }
17
Operator is vs. operator == on Type interface I { } class A { } class B : A { } class C : B, I { } class Program { static void Main(string[] args) { A x = new C(); Console.WriteLine(x is B); Console.WriteLine(x is C); Console.WriteLine(x is I); }
18
Operator is vs. operator == on Type interface I { } class A { } class B : A { } class C : B, I { } class Program { static void Main(string[] args) { A x = new C(); Console.WriteLine(x is B); Console.WriteLine(x is C); Console.WriteLine(x is I); Console.WriteLine(x.GetType() == typeof(B)); Console.WriteLine(x.GetType() == typeof(C)); Console.WriteLine(x.GetType() == typeof(I)); }
19
Operator is vs. operator == on Type interface I { } class A { } class B : A { } class C : B, I { } class Program { static void Main(string[] args) { A x = new C(); Console.WriteLine(x is B); Console.WriteLine(x is C); Console.WriteLine(x is I); Console.WriteLine(x.GetType() == typeof(B)); Console.WriteLine(x.GetType() == typeof(C)); Console.WriteLine(x.GetType() == typeof(I)); }
20
Operator is vs. operator == on Type interface I { } class A { } class B : A { } class C : B, I { } class Program { static void Main(string[] args) { A x = new C(); Console.WriteLine(x is B); Console.WriteLine(x is C); Console.WriteLine(x is I); Console.WriteLine(x.GetType() == typeof(B)); Console.WriteLine(x.GetType() == typeof(C)); Console.WriteLine(x.GetType() == typeof(I)); Console.WriteLine(x.GetType().IsSubclassOf(typeof(B))); Console.WriteLine(x.GetType().IsSubclassOf(typeof(C))); Console.WriteLine(x.GetType().IsSubclassOf(typeof(I))); }
21
Operator is vs. operator == on Type interface I { } class A { } class B : A { } class C : B, I { } class Program { static void Main(string[] args) { A x = new C(); Console.WriteLine(x is B); Console.WriteLine(x is C); Console.WriteLine(x is I); Console.WriteLine(x.GetType() == typeof(B)); Console.WriteLine(x.GetType() == typeof(C)); Console.WriteLine(x.GetType() == typeof(I)); Console.WriteLine(x.GetType().IsSubclassOf(typeof(B))); Console.WriteLine(x.GetType().IsSubclassOf(typeof(C))); Console.WriteLine(x.GetType().IsSubclassOf(typeof(I))); }
22
Operator As (returns nullable)
23
Method Overloading
24
Method Overloading and Fragile Base Class using System; public class A { public void f(int i) { Console.WriteLine("A.f(int)"); } public class B : A { public void f(double d) { Console.WriteLine("B.f(double)"); } public void Test() { f(1); } class Program { static void Main(string[] args) { B b = new B(); b.Test(); } Why?
25
Generic Methods
26
Null Values in Generic Types/Methods Setting a value to null void Foo () { T x = null;// error T y = 0;// error T z = default(T);// ok! 0, '\0', false, null } Comparing a value against null void Foo (T x) { if (x == null) { Console.WriteLine(true); } else { Console.WriteLine(false"); } Foo(3);// false Foo(0);// false Foo("Hello");// false Foo (null);// true for reference types x == null does a comparison for nullable types x == null does a comparison for value types x == null returns false
27
Overview of Constraints ConstraintDescription where T: struct The type argument must be a value type. Any value type except Nullable can be specified. where T : class The type argument must be a reference type, including any class, interface, delegate, or array type. where T : new() The type argument must have a public parameterless constructor. When used in conjunction with other constraints, the new() must be specified last. where T : BaseClassName The type argument must be or derive from the specified base class. where T : InterfaceName The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic. where T : U The type argument supplied for T must be or derive from the type argument supplied for U. This is called a naked type constraint.
28
Generic Methods Methods that can work with arbitrary data types static void Sort (T[] a) where T: IComparable { for (int i = 0; i < a.Length-1; i++) { for (int j = i+1; j < a.Length; j++) { if (a[j].CompareTo(a[i]) < 0) { T x = a[i]; a[i] = a[j]; a[j] = x; } can sort any array as long as the array elements implement IComparable Usage int[] a = {3, 7, 2, 5, 3};... Sort (a); // a == {2, 3, 3, 5, 7} string[] s = {"one", "two", "three"};... Sort (s); // s == {"one", "three", "two"} From the method parameters the compiler can usually infer the concrete type that is to be substituted for the placeholder type; so one can simply write: Sort(a); // a == {2, 3, 3, 5, 7}Sort(s); // s == {"one", "three", "two"}
29
Default constructor
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.