CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 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 4 th Lecture Pavel Ježek
Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming.
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.
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.
Programming in C# Language Overview
Performing Simple Calculations with C# Svetlin Nakov Telerik Corporation
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
.NET Data types. Introduction ٭ A "data type" is a class that is primarily used just to hold data. ٭ This is different from most other classes since they're.
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
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
3. Declaring Value-Type Variables
C Tokens Identifiers Keywords Constants Operators Special symbols.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
C#C# Introduction CS3260 Dennis A. Fairclough Version 1.0 Introduction CS3260 Dennis A. Fairclough Version 1.0.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Telerik Software Academy Telerik School Academy Integer, Floating-Point, Text Data, Variables,
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 5 th Lecture Pavel Ježek
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
C# - variablesBjørk Busch1 Variables Basic types (also defined as classes with methods) : Integer-types Int, long, byte, sbyte, short, ushort, uint, ulong.
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
Performing Simple Calculations with C# Telerik Corporation
CNS 3260 C# .NET Software Development
1.2 Primitive Data Types and Variables
Chapter One Lesson Three DATA TYPES ©
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
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.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Operators and Expressions
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 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.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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
Basic Introduction to C#
Advanced .NET Programming I 11th Lecture
Advanced .NET Programming II 4th Lecture
Module 5: Common Type System
Computing with C# and the .NET Framework
Chapter 2.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Dr Shahriar Bijani Winter 2017
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I UTPA – Fall 2012 This set of slides is revised from lecture slides.
Data Types Imran Rashid CTO at ManiWeber Technologies.
Module 2 Variables, Assignment, and Data Types
Java Programming Language
.NET Base Type (CTS Data Type) Managed Extensions for C++ Keyword
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 9th Lecture
C# Language & .NET Platform 12th Lecture
Presentation transcript:

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

How Would You Implement List.Clear() ? OptionComplexity with Respect to Number of Elements in List (n) AO(1) BO(n) CSomething else.

How Would You Implement List.Clear() ? OptionComplexity with Respect to Number of Elements in List (n) AO(1) BO(n) – set all elements to null ! Mind the GC! CSomething else.

What about String.Substring?

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

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 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? ) Interfaces are not inherited from System.Object, but System.Object members are callable/accessible via any interface type expression.

Example: Inheritance of private members

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

Ref. Type DOES NOT Imply Instances 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? )

Hiding Members can be declared as new in a subclass. They hide inherited members with the same name and signature. class A { public int x; public void F() {...} public virtual void G() {...} } class B : A { public new int x; public new void F() {...} public new void G() {...} } B b = new B(); b.x =...;// accesses B.x b.F();... b.G();// calls B.F and B.G ((A)b).x =...;// accesses A.x! ((A)b).F();... ((A)b).G(); // calls A.F and A.G!

What is the output the following program? class A { public string className = “A”; } class B : A { private string className = “B”; } class Program { static void Main(string[] args) { Console.WriteLine(new B().className); } OptionResult AIt will not compile – error in class B. BIt will not compile – error in class Program. CA DB EIt will generate a runtime error.

What is the output the following program? class A { public string className = “A”; } class B : A { private string className = “B”; } class Program { static void Main(string[] args) { Console.WriteLine(new B().className); } OptionResult AIt will not compile – error in class B. BIt will not compile – error in class Program. CA DB EIt will generate a runtime error. a compiler warning: use new keyword new

What is the output the following program? class A { public string className = “A”; } class B : A { private string className = “B”; } class Program { static void Main(string[] args) { Console.WriteLine(new B().className); } OptionResult AIt will not compile – error in class B. BIt will not compile – error in class Program. CA & a compiler warning: use new keyword. DB EIt will generate a runtime error.

What is the output the following program? class A { public string className = “A”; } class B : A { private string className = “B”; public void f() { Console.WriteLine(className); } class Program { static void Main(string[] args) { Console.Write(new B().className); new B().f(); } OptionResult AA BA B CB A DB

What is the output the following program? class A { public string className = “A”; } class B : A { private string className = “B”; public void f() { Console.WriteLine(className); } class Program { static void Main(string[] args) { Console.Write(new B().className); new B().f(); } OptionResult AA BA B & a compiler warning: use new keyword. CB A DB

Nested Types

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

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

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

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

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

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) 7 decimal digit precision 28 decimal digit precision 15 decimal digit precision

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

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

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 long 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: [ ] [E[+/-]123], e.g. 10.0, 10E15, 10E-4, A literal case-insensitive suffix forces literal type: f Ffloat d Ddouble m Mdecimal

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

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: ± * 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) Values in Decimals are not normalized: decimal a = 1; decimal b = M; 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 = ; double c = 1.00; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c);

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.

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.

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.

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

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.

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.

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) is a valid and unique double value, distinct from 0.0.