Sei sulla pagina 1di 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
Object
User-defined types

String Array ValueType Exception Delegate Class1

Primitive types Multicast


Enum Structure1 Class2
Delegate
Boolean Single

Byte Double

Int16 Decimal Enum1 Delegate1 Class3

Int32 DateTime

Int64 TimeSpan

Char Guid

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

public class App


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

decl + initializer int area = width * height;

int x;
error, x not set int y = x * 2;
...
}
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
int i = 5;
double d = 3.2;
string s = "496"; //String representation of a number

int j, k;
implicit conversion d = i;

typecast required i = (int) d;


conversion required i = int.Parse(s); //32-bit signed integer
j = System.int32.Prase(s);
k = System.Convert.ToInt32(d); //32-bit signed
integer

Microsoft 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; 10
i = 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 i;
string s;
double d;
int[] a;

i = 10; //Value
s = "calico"; // Ref
d = 3.14159; // Value
a = new int[100]; //Ref

Microsoft 11
Boxing and Unboxing

• When necessary, C# will auto-convert value <==> object


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

Microsoft 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; // 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; }
}

Microsoft 14
Creating objects

• Objects are created using the New operator

Customer c1, c2;


string 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 c1, c2, c3;


string 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) ... // exception: == overloaded to s1.Equals(s2)

Microsoft 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; // type-cast to access id


return this.ID == other.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.)
int[] a;
create a = new int[5];

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

number of elements int l = a.Length;

Microsoft 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;
public struct Point
{
public int x;
public int y;
}

Microsoft 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;
A2[0].y = 100; public class Point
{
public int x;
public int 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[,] twoD;
int[][] 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

Potrebbero piacerti anche