Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Objectives
Microsoft 2
Part 1
Microsoft 3
The Common Type System (CTS)
System-defined types
Object
User-defined types
Byte Double
Int32 DateTime
Int64 TimeSpan
Char Guid
Microsoft 4
The Common Language Specification (CLS)
Microsoft 5
Mapping C# to CTS
• Language keywords map to common CTS classes:
int x;
error, x not set int y = x * 2;
...
}
Microsoft } 7
Type conversion
int j, k;
implicit conversion d = i;
Microsoft 8
Part 2
Microsoft 9
Value vs. reference types
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?
• 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
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…
Microsoft 13
Classes
Microsoft 14
Creating objects
– strings are a special case and don't require the use of new…
Microsoft 15
Working with reference types
Microsoft 16
Defining equality
Microsoft 17
GetHashCode
Microsoft 18
Part 4
• Arrays…
Microsoft 19
Arrays
a[0] = 17;
element access a[1] = 32;
int x = a[0] + a[1] + a[4];
Microsoft 20
Arrays of value types
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
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
Customer[,] twoD;
int[][] jagged2D;
jagged2D[0][0] = 1;
jagged2D[9][99] = 100;
Microsoft 23
Summary
Microsoft 24