1 / 23

C#: Data Types

C#: Data Types. Based on slides by Joe Hummel. Content:.

affrica
Download Presentation

C#: Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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. Part 1 • The Common Type System…

  4. The Common Type System (CTS) • CTS is based the class hierarchy defined in FCL • all types are sub types to Object (except interfaces)

  5. 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. Mapping C# onto CTS • Language keywords map to common CTS classes:

  7. 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, xis not initialised

  8. 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. Part 2 • Value types vs. reference types…

  10. "calico" 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";

  11. 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) Like Java “a message” i: int i; string s; Customer c1, c2; i = 23; s = "a message"; c1 = null; c2 = new Customer(…); 23 null s: c1: c2: CNo:... CName:... ...

  12. Boxing and Unboxing Like Java • 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!

  13. 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. Using class types… • Instantiation, assignment, and comparison: Customer c1, c2, c3; string s1, s2; c1 = new Customer("joehummel", 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. 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. Part 3 • Arrays…

  17. 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; Create Element access Size (no of elements)

  18. 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 sizeon the second dimension Differentsizeson dimension 2

  19. Part 4 • Namespaces…

  20. 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. Example • Framework Class Library (FCL) includes thousands of classes • How are they organised? • How is name clashes avoided? • with FCL? • inside FCL?

  22. FCL namespaces • FCL: top namespace is "System" • FCL technologies are embedded in System…

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

More Related