03 Data Types

  • Uploaded by: api-19624280
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 03 Data Types as PDF for free.

More details

  • Words: 1,385
  • Pages: 24
Data Types

Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different languages, to work together. To ensure interoperability across languages, Microsoft has also defined the CLS, or Common Language Specification, a subset of the CTS that all languages support. Otherwise, the types in C# are what you would expect from a modern OOPL…”

• • • •

The Common Type System Value vs. reference types Classes Arrays

Microsoft

2

Part 1 •

The Common Type System…

Microsoft

3

The Common Type System (CTS) •

CTS denotes one type system for all languages – every type is based on a class in the FCL (i.e. fully-OOP!) – all types inherit from Object System-defined types Obj ect

User-defined types

St r i ng

Ar r ay

Pr i mi t i ve t ypes

Microsoft

Bool ean

Si ngl e

Byt e

Doubl e

I nt 16

Deci mal

I nt 32

Dat eTi me

I nt 64

Ti meSpan

Char

Gui d

Val ueType

Except i on

Del egat e

Cl ass1

Enum

St r uct ur e1

Mul t i cast Del egat e

Cl ass2

Del egat e1

Cl ass3

Enum1

4

The Common Language Specification (CLS) •

Not all languages support all CTS types and features – C# supports unsigned integer types, VB.NET does not – C# is case sensitive, VB.NET is not – C# supports pointer types (in unsafe mode), VB.NET does not – C# supports operator overloading, VB.NET does not



CLS was drafted to promote language interoperability – vast majority of classes within FCL are CLS-compliant

Microsoft

5

Mapping C# to CTS •

Language keywords map to common CTS classes: Keyword

Description

Special format for literals

bool

Boolean

true false

char

16 bit Unicode character

'A' '\x0041' '\u0041'

sbyte

8 bit signed integer

none

byte

8 bit unsigned integer

none

short

16 bit signed integer

none

ushort

16 bit unsigned integer

none

int

32 bit signed integer

none

uint

32 bit unsigned integer

U suffix

long

64 bit signed integer

L or l suffix

ulong

64 bit unsigned integer

U/u and L/l suffix

float

32 bit floating point

F or f suffix

double

64 bit floating point

no suffix

decimal

128 bit high precision

M or m suffix

string

character sequence

"hello", @"C:\dir\file.txt"

Microsoft

6

Example •

An example of using types in C# – declare before you use (compiler enforced) – initialize before you use (compiler enforced)

declarations

public class App { public static void Main() { int width, height; width = 2; height = 4; int area = width * height;

decl + initializer

int x; int y = x * 2; ...

error, x not set

} Microsoft

}

7

Type conversion • •

Some type conversions are automatic – from smaller to larger types Otherwise you need a cast or an explicit conversion… – type-cast syntax is type name inside parentheses – conversions based on FCL classes

implicit conversion typecast conversion required required

int i = 5; double d = 3.2; string s = "496"; int j, k; d = i; i i j k

= = = =

integer

Microsoft

//String representation of a number

(int) d; int.Parse(s); //32-bit signed integer System.int32.Prase(s); System.Convert.ToInt32(d); //32-bit

signed

8

Part 2 •

Value vs. reference types…

Microsoft

9

Value vs. reference types • •

.NET separates data types into two categories Value types: – variable represents a value ("bits") int i; i = 10;



10

Reference types: – variable represents a reference to a heap-based object – actual data resides in the object

"calico"

string s; s = "calico";

Microsoft

10

How do you know which types are which? • •

Types that inherit from ValueType are values All other types are references



Examples: – let's work through the following… int string double int[] i s d a

Microsoft

= = = =

i; s; d; a;

10; //Value "calico"; // Ref 3.14159; // Value new int[100]; //Ref

11

Boxing and Unboxing •

When necessary, C# will auto-convert value <==> object – value ==> object is called "boxing" – object ==> value is called "unboxing" int object string i obj i j

= = = =

i, j; obj; s; 32; i; 19; (int) obj;

s = j.ToString(); s = 99.ToString();

Microsoft

// boxed copy! // unboxed! // boxed! // boxed!

12

Part 3… •

User-defined reference types…

Microsoft

13

Classes • •

Classes yield user-defined reference types Example: – Customer class public class Customer { public string Name; public int ID;

// fields

public Customer(string name, int id) { this.Name = name; this.ID = id; }

// constructor

public override string ToString() { return "Customer: " + this.Name; }

// method

} Microsoft

14

Creating objects •

Objects are created using the New operator Customer string

c1, c2; s1, s2;

c1 = new Customer("jim bag", 36259); c2 = new Customer("jane doe", 55298); s1 = "an apple a day"; s2 = "keeps the doctor away";

– strings are a special case and don't require the use of new…

Microsoft

15

Working with reference types •

Creating, assigning, and comparing – let's work through the following… Customer string

c1, c2, c3; s1, s2;

c1 = new Customer("jim bag", 36259); c2 = new Customer("jane doe", 55298); c3 = null; // c3 references no object c3 = c1;

// c3 references same obj as c1

if (c1 == null) ... // does c1 ref an object? if (c1 == c2) ... // compares references if (c1.Equals(c2)) ... // compares objects if (s1 == s2) ... Microsoft

// exception: == overloaded to s1.Equals(s2) 16

Defining equality • •

Classes should override Equals Example: – Customers are equal if their IDs are the same public class Customer { . . . public override bool Equals(object obj) { Customer other; if ((obj == null) || (!(obj is Customer))) return false; // definitely not equal other = (Customer) obj; return this.ID == other.ID;

// type-cast to access id // equal if same id...

} Microsoft

17

GetHashCode • •

If you override Equals, must also override GetHashCode In .NET, GetHashCode is a cheap test for equivalence – obj1.GetHashCode() != obj2.GetHashCode() ==> not equal – obj1.GetHashCode() == obj2.GetHashCode() ==> unknown public class Customer { . . . public override int GetHashCode() { // delegate hash code computation to underlying integer class… return this.ID.GetHashCode(); }

Microsoft

18

Part 4 •

Arrays…

Microsoft

19

Arrays •

Arrays are reference types – based on Array class in FCL – must be created using new – 0-based indexing – assigned default values (0 for numeric, null for references, etc.) creat e element access number of elements

Microsoft

int[] a; a = new int[5]; a[0] = 17; a[1] = 32; int x = a[0] + a[1] + a[4]; int l = a.Length;

20

Arrays of value types •

Value types yield preallocated data within array itself… – what does the situation look like in memory? int[] A1; A1 = new int[10]; A1[0] = 99; Point[] A2; A2 = new Point[10]; A2[0].x = 100; A2[0].y = 100;

Microsoft

public struct Point { public int x; public int y; } 21

Arrays of reference types •

Reference types yield references to objects outside array… – now what does the situation look like in memory? string[] A1; A1 = new string[10]; A1[0] = "apple"; Point[] A2; A2 = new Point[10]; A2[0] = new Point(); A2[0].x = 100; public class Point A2[0].y = 100; {

public int public int

x; y;

} Microsoft

22

Multi-dimensional arrays •

C# supports arrays as a single object OR array of arrays – latter allows you to implement jagged arrays Customer[,] int[][]

twoD; jagged2D;

// 2D array as single object

twoD = new Customer[10, 100]; twoD[0, 0] = new Customer(…); twoD[9, 99] = new Customer(…); // 2D array as array of arrays

jagged2D = new int[10][]; jagged2D[0] = new int[10]; jagged2D[1] = new int[20]; jagged2D[9] = new int[100]; jagged2D[0][0] = 1; jagged2D[9][99] = 100; Microsoft

23

Summary •

CTS is the common type system – same type system for all languages – every type represented by underlying class in FCL – fundamental difference between value & reference types



CLS is the common language specification – types that are guaranteed to work across languages

Microsoft

24

Related Documents

03 Data Types
June 2020 4
Data Types
November 2019 18
Oracle Data Types
May 2020 5
11 Advanced Data Types
November 2019 12
Data Colection Types
April 2020 6
Sql Data Types
November 2019 8