Types: © University of Linz, Institute For System Software, 2004 Published Under The Microsoft Curriculum License
Types: © University of Linz, Institute For System Software, 2004 Published Under The Microsoft Curriculum License
2
Value Types and Reference Types
Value Types Reference Types
variable contains value reference
stored on stack (or in an object) heap
initialization 0, false, '\0' null
assignment copies the value copies the reference
i 17 s
Hello
j 17 s1
3
Primitive Types
long form in Java range
sbyte System.SByte byte -128 .. 127
byte System.Byte --- 0 .. 255
short System.Int16 short -32768 .. 32767
ushort System.UInt16 --- 0 .. 65535
int System.Int32 int -231 .. 231-1
uint System.UInt32 --- 0 .. 232 -1
long System.Int64 long -263 .. 263-1
ulong System.UInt64 --- 0 .. 264-1
float System.Single float ±1.5E-45 .. ±3.4E38 (32 Bit)
double System.Double double ±5E-324 .. ±1.7E308 (64 Bit)
decimal System.Decimal --- ±1E-28 .. ±7.9E28 (128 Bit)
bool System.Boolean boolean true, false
char System.Char char Unicode character
4
Type decimal
128 bit floating point type
s = 0 or 1
(-1) * m * 10
s -e
0 ≤ m < 296
0 ≤ e ≤ 28
5
Compatibility Between Primitive Types
char
6
Enumerations
List of named constants
Usage
7
Operations on Enumerations
Valid operations
comparisons if (c == Color.Red) ...
if (c > Color.Red && c <= Color.Green) ...
+, - c = c + 2;
++, -- c++;
& if ((c & Color.Red) == 0) ...
| c = c | Color.Blue;
~ c = ~ Color.Red;
The compiler does not check if the result is a valid enumeration value.
Note
- Enumerations cannot be assigned to int and vice versa (except after a type cast).
- Enumeration types inherit from object (Equals, ToString, ...).
- Class System.Enum (base type of all enumeration types) provides
operations on enumerations (GetName, Format, GetValues, ...).
8
Arrays
One-dimensional arrays
int[] a = new int[3];
int[] b = new int[] {3, 4, 5};
int[] c = {3, 4, 5};
SomeClass[] d = new SomeClass[10]; // array of references
SomeStruct[] e = new SomeStruct[10]; // array of values (directly in the array)
9
Multidimensional Arrays
Jagged (like in Java) a[0][1]
a
int[][] a = new int[2][]; a[0]
a[0] = new int[3]; a[1]
a[1] = new int[4];
int x = a[0][1];
10
Other Array Properties
Indexes start at 0
Array length
int[] a = new int[3];
Console.WriteLine(a.Length); // 3
int[][] b = new int[3][];
b[0] = new int[4];
Console.WriteLine("{0}, {1}", b.Length, b[0].Length); // 3, 4
int[,] c = new int[3, 4];
Console.WriteLine(c.Length); // 12
Console.WriteLine("{0}, {1}", c.GetLength(0), c.GetLength(1)); // 3, 4
11
Class System.String
Can be used as the standard type string
string s = "Alfonso";
Note
• Strings are immutable (use StringBuilder if you want to modify strings)
• Can be concatenated with +: "Don " + s
• Can be indexed: s[i]
• String length: s.Length
• Strings are reference types => reference semantics in assignments
• but their values can be compared with == and != : if (s == "Alfonso") ...
• Class String defines many useful operations:
CompareTo, IndexOf, StartsWith, Substring, ...
12
Variable-length Arrays
using System;
using System.Collections;
class Test {
Output
Alpha
Charly
Delta
13
Associative Arrays
using System;
using System.Collections;
class Test {
Output
Karin = 7131
Peter = 7130
Wolfgang = 7132
14
Structs
Declaration
struct Point {
public int x, y; // fields
public Point (int x, int y) { this.x = x; this.y = y; } // constructor
public void MoveTo (int a, int b) { x = a; y = b; } // methods
}
Usage
Point p; // still unititialized
Point p = new Point(3, 4); // constructor initializes object on the stack
p.x = 1; p.y = 2; // field access
p.MoveTo(10, 20); // method call
Point q = p; // value assignment of objects (all fields are assigned)
Note
• Structs are value types!
A struct declaration allocates an object directly on the stack or within some other object.
• Structs must not declare a parameterless constructor (they have one by default).
However, they may use it: p = new Point(); // initializes fields to 0, null, false, ...
15
Classes
Declaration
class Rectangle {
Point origin;
public int width, height;
public Rectangle() { origin = new Point(0,0); width = height = 0; }
public Rectangle (Point p, int w, int h) { origin = p; width = w; height = h; }
public void MoveTo (Point p) { origin = p; }
}
Usage
Rectangle r = new Rectangle(new Point(10, 20), 5, 5);
int area = r.width * r.height;
r.MoveTo(new Point(3, 3));
Rectangle r1 = r ; // reference assignment
Note
• Classes are reference types;
Their objects are allocated on the heap.
• The "new" operator allocates an object and calls its constructor.
Classes may declare a parameterless constructor.
16
Differences Between Classes and Structs
Classes Structs
17
Class System.Object
Base class of all reference types
class Object {
public virtual bool Equals(object o) {...}
public virtual string ToString() {...}
public virtual int GetHashCode() {...}
...
}
Assignment compatibility
obj = new Rectangle();
obj = new int[3];
Boxing
The assignment
object obj = 3;
wraps up the value 3 in a heap object
obj
Unboxing
The assignment
int x = (int) obj;
unwraps the value again
19
Boxing/Unboxing
Allows the implementation of "generic" container types
class Queue {
...
public void Enqueue(object x) {...}
public object Dequeue() {...}
...
}
This Queue can then be used for reference types and value types
Queue q = new Queue();
q.Enqueue(new Rectangle());
q.Enqueue(3);