Presentation is loading. Please wait.

Presentation is loading. Please wait.

C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science - 2012 Content: “.NET is designed around the CTS, or Common Type System.

Similar presentations


Presentation on theme: "C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science - 2012 Content: “.NET is designed around the CTS, or Common Type System."— Presentation transcript:

1 C#: Data Types Based on slides by Joe Hummel

2 2 UCN 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

3 3 UCN Technology: Computer Science - 2012 Part 1 The Common Type System…

4 4 UCN 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)

5 5 UCN 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

6 6 UCN Technology: Computer Science - 2012 Mapping C# onto CTS Language keywords map to common CTS classes: KeywordDescriptionSpecial format for literals bool Boolean true false char 16 bit Unicode character 'A' '\x0041' '\u0041' sbyte 8 bit signed integernone byte 8 bit unsigned integernone short 16 bit signed integernone ushort 16 bit unsigned integernone int 32 bit signed integernone 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 pointno suffix decimal 128 bit high precision M or m suffix string character sequence "hello", @"C:\dir\file.txt"

7 7 UCN 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

8 8 UCN 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

9 9 UCN Technology: Computer Science - 2012 Part 2 Value types vs. reference types…

10 10 UCN 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"

11 11 UCN 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

12 12 UCN 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

13 13 UCN 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; } }

14 14 UCN 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

15 15 UCN 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... }

16 16 UCN Technology: Computer Science - 2012 Part 3 Arrays…

17 17 UCN 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)

18 18 UCN 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 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; Same size on the second dimension Different sizes on dimension 2

19 19 UCN Technology: Computer Science - 2012 Part 4 Namespaces…

20 20 UCN 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

21 21 UCN 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?

22 22 UCN Technology: Computer Science - 2012 FCL namespaces FCL: top namespace is "System" FCL technologies are embedded in System… NamespacePurposeAssembly SystemCore classes, typesmscorlib.dll System.Collections Data structures (deprecated) mscorlib.dll System.Collections.GenericData structuresmscorlib.dll System.DataDatabase accessSystem.Data.dll System.Windows.FormsGUISystem.Windows.Forms.dll System.XMLXML processingSystem.Xml.dll

23 23 UCN 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


Download ppt "C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science - 2012 Content: “.NET is designed around the CTS, or Common Type System."

Similar presentations


Ads by Google