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

Slides:



Advertisements
Similar presentations
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Advertisements

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 14 th Lecture Pavel Ježek
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
3. Data Types. 2 Microsoft Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Equality Programming in C# Equality CSE / ECE 668 Prof. Roger Crawfis.
Advanced .NET Programming I 13th Lecture
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
ITF11006.NET The Basics. Data Types – Value Types – Reference Types Flow Control – Conditional – Loop Miscellaneous – Enumerations – Namespaces Class.
Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Neal Stublen Tonight’s Agenda  Indexers  Delegates and events  Operator overloading  Class inheritance  Q&A.
ITF11006.NET Generics. Generics - Sample Recent Generics - Characteristics Performance Type Safety Binary Code Reuse Code Bloat Naming Guidelines.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 8 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 9 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
CNS 3260 C# .NET Software Development
Session 1 C# Basics.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
- This slide is intentionally left blank - Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek
1 Statements © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 5 th Lecture Pavel Ježek
1 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System.
Object Oriented Programming Lecture 2: BallWorld.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 3 rd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 7 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 2 nd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Advanced .NET Programming I 3nd Lecture
Advanced .NET Programming II 6th Lecture
Advanced .NET Programming II 4th Lecture
Module 5: Common Type System
Advanced .NET Programming I 7th Lecture
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Advanced .NET Programming I 8th Lecture
Advanced .NET Programming I 5th Lecture
C# Language & .NET Platform 10th Lecture
- This slide is intentionally left blank -
Advanced .NET Programming I 3rd Lecture
C# Language & .NET Platform 11th Lecture
Advanced .NET Programming I 6th Lecture
C# Language & .NET Platform 3rd Lecture
C# Language & .NET Platform 9th Lecture
C# Language & .NET Platform 4th Lecture
C# Language & .NET Platform 8th Lecture
C# Language & .NET Platform 12th Lecture
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 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 (

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

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

Enums

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

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

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 }

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

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

Nullable Types, bool?

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

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

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

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

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); }

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); }

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); }

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)); }

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)); }

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))); }

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))); }

Operator As (returns nullable)

Method Overloading

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?

Generic Methods

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

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.

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

Default constructor