23
C#: Data Types Based on slides by Joe Hummel

C#: Data Types

  • Upload
    affrica

  • View
    31

  • Download
    0

Embed Size (px)

DESCRIPTION

C#: Data Types. Based on slides by Joe Hummel. Content:. - PowerPoint PPT Presentation

Citation preview

Page 1: C#:  Data Types

C#: Data Types

Based on slides by Joe Hummel

Page 2: C#:  Data Types

2UCN Technology: Computer Science - 2012

Content:

“.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• Arrays• Namespaces

Page 3: C#:  Data Types

3UCN Technology: Computer Science - 2012

Part 1

• The Common Type System…

Page 4: C#:  Data Types

4UCN Technology: Computer Science - 2012

The Common Type System (CTS)

• CTS is based the class hierarchy defined in FCL– all types are sub types to Object (except interfaces)

St r i ng Ar r ay Val ueType Except i on Del egat e Cl ass1

Mul t i castDel egat e Cl ass2

Cl ass3

Obj ect

Enum1

St r uct ur e1EnumPr i mi t i ve t ypes

Bool ean

Byt e

I nt 16

I nt 32

I nt 64

Char

Si ngl e

Doubl e

Deci mal

Dat eTi me

System-defined types

User-defined types

Del egat e1

Ti meSpan

Gui d

Page 5: C#:  Data Types

5UCN Technology: Computer Science - 2012

The Common Language Specification (CLS)

• Not all .NET languages support all CTS types and properties– C# supports unsigned integer, VB.NET doesn’t– C# is case sensitive, VB.NET doesn’t– C# supports pointers (in unsafe mode), VB.NET doesn’t– C# supports operator overloading, VB.NET doesn’t

• The intension of CLS help integration of code written in different languages– The majority of the classes in FCL is in accordance with CLS

Page 6: C#:  Data Types

6UCN Technology: Computer Science - 2012

Mapping C# onto CTS• Language keywords map to common CTS classes:

Keyword Description Special format for literals

bool Boolean true falsechar 16 bit Unicode character 'A' '\x0041' '\u0041'sbyte 8 bit signed integer nonebyte 8 bit unsigned integer noneshort 16 bit signed integer noneushort 16 bit unsigned integer noneint 32 bit signed integer noneuint 32 bit unsigned integer U suffixlong 64 bit signed integer L or l suffixulong 64 bit unsigned integer U/u and L/l suffixfloat 32 bit floating point F or f suffixdouble 64 bit floating point no suffixdecimal 128 bit high precision M or m suffixstring character sequence "hello", @"C:\dir\file.txt"

Page 7: C#:  Data Types

7UCN Technology: Computer Science - 2012

Example

• An example of types in C#– Variable must be declared (compiler-checked)– Variable must be initialised (compiler-checked)

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

int area = width * height;

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

Declaration

Declaration + initialisation

Error, x is not initialised

Page 8: C#:  Data Types

8UCN Technology: Computer Science - 2012

Type conversion (typecast)

• Implicit type conversion:– from “smaller” to “larger” type

• Otherwise explicit typecast or conversion…– Typecast: target type in parenthesis– Converting is based on the System.Convert class

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

d = i;

i = (int) d;

i = System.Convert.ToInt32(s);

implicit cast

Typecast is needed

Conversion is needed

Page 9: C#:  Data Types

9UCN Technology: Computer Science - 2012

Part 2

• Value types vs. reference types…

Page 10: C#:  Data Types

10UCN Technology: Computer Science - 2012

Value types vs. reference types

• C# separates data types into two categories• Value types:

– The variable holds a value ("bits")

• Reference types:– The variable holds a reference (address) to an object– The actual values are embedded in the object

int i;i = 10;

10

string s;s = "calico";

"calico"

Page 11: C#:  Data Types

11UCN Technology: Computer Science - 2012

How does one know what is what?

• Learn it by heart!• But it isn’t that difficult:

– primitive types as bool, int and double are value types– The rest is reference types (except structs)

int i;string s;Customer c1, c2;

i = 23;s = "a message";c1 = null;c2 = new Customer(…);

Like Java

“a message”

CNo:...CName:......

s:

c1:c2:

i: 23

null

Page 12: C#:  Data Types

12UCN Technology: Computer Science - 2012

Boxing and Unboxing

• C# converts value <==> object when needed– Value ==> object is called "boxing"– object ==> value is called "unboxing"

int i, j;object obj;string s;

i = 32;obj = i; // boxed copy!i = 19;j = (int) obj; // unboxed!

s = j.ToString(); // boxed!s = 99.ToString(); // boxed!

Like Java

Page 13: C#:  Data Types

13UCN Technology: Computer Science - 2012

User-defined reference types(Abstract data types or… classes)• For instance a Customer class…

public class Customer{ public string name; // fields public int id;

public Customer(string name, int id) // constructor { this.name = name; this.id = id; }

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

Page 14: C#:  Data Types

14UCN Technology: Computer Science - 2012

Using class types…

• Instantiation, assignment, and comparison:

Customer c1, c2, c3;string s1, s2;

c1 = new Customer("joe hummel", 36259);c2 = new Customer("marybeth lore", 55298);c3 = null; // c3 references no object

c3 = c1; // c3 now references same obj as c1

if (c1 == null) ... // do I ref an object? if (c1 == c2) ... // compares references if (c1.Equals(c2)) ... // compares objects

if (s1 == s2) ... // exception: == overloaded to // compare string data

Page 15: C#:  Data Types

15UCN Technology: Computer Science - 2012

Defining “Equal to”

• Classes ought to re-define Equals

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; // typecast to access return this.id == other.id; // equal if same id... }

Page 16: C#:  Data Types

16UCN Technology: Computer Science - 2012

Part 3

• Arrays…

Page 17: C#:  Data Types

17UCN Technology: Computer Science - 2012

Arrays

• Arrays is reference types– based on the Array class in FCL– crated using new– 0-based index– Assigned default values (0 for numeric, null for references,

etc.)

int[] a;a = new int[5];

a[0] = 17;a[1] = 32;int x = a[0] + a[1] + a[4];

int l = a.Length;

Element access

Create

Size (no of elements)

Page 18: C#:  Data Types

18UCN Technology: Computer Science - 2012

Multi-dimensional Arrays• C# supports arrays as a single object OR as array of arrays

– this implements a 2D array (matrix) with different sizes for the two dimensions (twoD) and a 2D array (Jagged)with varying sizes on the second dimension

Customer[,] twoD;int[][] jagged2D;

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

// 2D array as array of arraysjagged2D = 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;

Same size on the second dimension

Different sizes on dimension 2

Page 19: C#:  Data Types

19UCN Technology: Computer Science - 2012

Part 4

• Namespaces…

Page 20: C#:  Data Types

20UCN Technology: Computer Science - 2012

Namespaces

• Namespaces used for organising classes– a namespace N is a set of classes within the scope of N.– namespaces are often embedded.

namespace Workshop{ public class Customer { . . . }

public class Product { . . . }}//namespace

Workshop.Customer

Page 21: C#:  Data Types

21UCN Technology: Computer Science - 2012

Example

• Framework Class Library (FCL) includes thousands of classes– How are they organised?– How is name clashes avoided?

• with FCL?• inside FCL?

Page 22: C#:  Data Types

22UCN Technology: Computer Science - 2012

FCL namespaces

• FCL: top namespace is "System"• FCL technologies are embedded in System…

Namespace Purpose Assembly

System Core classes, types mscorlib.dll

System.Collections Data structures (deprecated) mscorlib.dll

System.Collections.Generic Data structures mscorlib.dll

System.Data Database access System.Data.dll

System.Windows.Forms GUI System.Windows.Forms.dll

System.XML XML processing System.Xml.dll

Page 23: C#:  Data Types

23UCN Technology: Computer Science - 2012

Summing Up

• CTS is the common type system– same type system for all .NET languages– types are implemented using FCL classes– Simple data types use call by value, classes use call by

reference• CLS is the common language specification

– types that are guaranteed to work across languages

• Don’t get namespaces and assemblies mixed up…– namespaces help organising source code– assemblies are for implementation / packaging