Escolar Documentos
Profissional Documentos
Cultura Documentos
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 Arrays Namespaces
Microsoft
Part 1
The Common Type System
Microsoft
String
Array
ValueType
Exception
Delegate
Class1
Primitive types Boolean Byte Int16 Int32 Int64 Char Single Double Decimal DateTime TimeSpan Guid
Enum
Structure1
Multicast Delegate
Class2
Enum1
Delegate1
Class3
Microsoft
Microsoft
Mapping C# to CTS
Language keywords map to common CTS classes:
Keyword
bool char sbyte byte short ushort int uint long ulong float double decimal string Microsoft
Description
Boolean 16 bit Unicode character 8 bit signed integer 8 bit unsigned integer 16 bit signed integer 16 bit unsigned integer 32 bit signed integer 32 bit unsigned integer 64 bit signed integer 64 bit unsigned integer 32 bit floating point 64 bit floating point 128 bit high precision character sequence
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() { int width, height; width = 2; height = 4; int area = width * height; int x; int y = x * 2; ... }
Microsoft
declarations
decl + initializer
Type conversion
Some automatic type conversions available from smaller to larger types Otherwise you need a cast or an explicit conversion typecast syntax is type name inside parentheses conversion based on System.Convert class
int double string
implicit conversion typecast required conversion required
i = 5; d = 3.2; s = "496";
d = i; i = (int) d;
i = System.Convert.ToInt32(s);
Microsoft
Part 2
Value vs. reference types
Microsoft
Reference types: variable represents a reference to a heap-based object actual data resides in the object
"calico" string s; s = "calico";
Microsoft
10
Microsoft
11
s = j.ToString(); s = 99.ToString();
Microsoft
12
// fields
public Customer(string name, int id) { this.Name = name; this.ID = id; } public override string ToString() { return "Customer: " + this.Name; }
}
// constructor
// method
Microsoft
13
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 // do I ref an object? // compares references // compares objects
if (c1 == null) ... if (c1 == c2) ... if (c1.Equals(c2)) ... if (s1 == s2) ...
Microsoft
Defining equality
Classes should override 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; return this.ID == other.ID; // typecast to access // equal if same id...
Microsoft
15
GetHashCode
If you override Equals, must also override GetHashCode:
public class Customer { . . . public override int GetHashCode() { return this.id.GetHashCode(); }
Microsoft
16
Part 3
Arrays
Microsoft
17
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; a = new int[5];
a[0] = 17; a[1] = 32; int x = a[0] + a[1] + a[4]; int l = a.Length;
create
element access
number of elements
Microsoft
18
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;
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 19
Part 4
Namespaces
Microsoft
20
Namespaces
Namespaces are a means for organizing types a namespace N is a set of names scoped by N namespaces are often nested
namespace Workshop { public class Customer { . . . }
Workshop.Customer
Example
Framework Class Library (FCL) contains 1000's of classes how to organize? how to avoid name collisions? with FCL within FCL
Microsoft
22
FCL namespaces
FCL's outermost namespace is "System" FCL technologies nested within System
Namespace
System System.Collections
Purpose
Core classes, types Data structures
Assembly
mscorlib.dll mscorlib.dll
Microsoft
23
Namespace != Assembly
Orthogonal concepts: namespace for organization assembly for packaging
One namespace could be spread across multiple assemblies One assembly may contain multiple namesspaces e.g. mscorlib.dll
Microsoft
24
Summary
CTS is the common type system same type system for all languages types implemented by classes in FCL fundamental difference between value & reference types CLS is the common language specification types that are guaranteed to work across languages
Try not to confuse namespaces with assemblies namespaces help with organization assemblies denote implementation / packaging
Microsoft
25
References
Books: I. Pohl, "C# by Dissection" S. Lippman, "C# Primer" J. Mayo, "C# Unleashed"
Microsoft
26