Date post: | 18-Dec-2015 |
Category: |
Documents |
Upload: | philippa-mills |
View: | 219 times |
Download: | 0 times |
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
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
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
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 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"
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
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
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"
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
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
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; }}
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
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... }
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)
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
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
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?
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
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