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

Slides:



Advertisements
Similar presentations
2. C# Language Fundamentals
Advertisements

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 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.
Types and Variables. Computer Programming 2 C++ in one page!
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.
Performing Simple Calculations with C# Svetlin Nakov Telerik Corporation
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Simple Data Type Representation and conversion of numbers
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.
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
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.
CS_OOP Lecture #2: Computer Hardware/Software; Variables, C# Data Types, and IO.
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
Chapter 2: Using Data.
C++ Character Set It is set of Characters/digits/symbol which is valid in C++. Example – A-Z, (white space) C++ Character Set It is set of.
 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.
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,
Copyright Curt Hill Variables What are they? Why do we need them?
Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.
Chapters 2 & 3. .NET Software development model that allows applications created in disparate programming languages to communicate Universal data access.
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.
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 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 9 th Lecture Pavel Ježek
Performing Simple Calculations with C# Telerik Corporation
CSC 298 Streams and files.
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
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
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
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
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 II 2 nd Lecture Pavel Ježek
Basic Introduction to C#
Advanced .NET Programming I 11th Lecture
Numeral Types and Type Conversion
Computing with C# and the .NET Framework
Chapter 2.
An overview of Java, Data types and variables
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall 2012 This set of slides is revised from lecture.
Data Types Imran Rashid CTO at ManiWeber Technologies.
The important features of OOP related to C#:
Module 2 Variables, Assignment, and Data Types
C# Language & .NET Platform 10th Lecture
- This slide is intentionally left blank -
C# Language & .NET Platform 11th Lecture
C# Language & .NET Platform 9th Lecture
C# Language & .NET Platform 12th Lecture
ECE 120 Midterm 1 HKN Review Session.
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

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

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

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

Visibility Visibility modifiers: publicAccess is not restricted. protectedAccess is limited to the containing class or types derived from the containing class. internalAccess is limited to the current assembly. protected internalAccess is limited to the current assembly or types derived from the containing class. privateAccess is limited to the containing type. Default visibility in: enumpublic classprivate interfacepublic structprivate

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.