CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 9 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
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
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.
The Type System1. 2.NET Type System The type system is the part of the CLR that defines all the types that programmers can use, and allows developers.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
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.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Programming in C# Language Overview
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
Advanced .NET Programming I 13th Lecture
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
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.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
CISC105 – General Computer Science Class 9 – 07/03/2006.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
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.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
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.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
COMP Primitive and Class Types Yi Hong May 14, 2015.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
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
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
Object Oriented Software Development 4. C# data types, objects and references.
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
- This slide is intentionally left blank - Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System.
Operators and Expressions
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 5 th Lecture Pavel Ježek
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Object Oriented Programming Lecture 2: BallWorld.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 3 rd Lecture Pavel Ježek
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 2 nd Lecture Pavel Ježek
Advanced .NET Programming I 3nd Lecture
Advanced .NET Programming II 4th Lecture
Module 5: Common Type System
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
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
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
Presentation transcript:

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

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)

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

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

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.

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)

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

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)

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"]);

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"]);

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.