The document discusses the Common Type System and Common Language Specification in .NET. It explains that the CTS defines a common type system for all languages with all types based on classes in the Framework Class Library. The CLS specifies a subset of the CTS that is supported across all languages to ensure interoperability. The document also provides examples of value types, reference types, boxing and unboxing in C#.
The document discusses the Common Type System and Common Language Specification in .NET. It explains that the CTS defines a common type system for all languages with all types based on classes in the Framework Class Library. The CLS specifies a subset of the CTS that is supported across all languages to ensure interoperability. The document also provides examples of value types, reference types, boxing and unboxing in C#.
The document discusses the Common Type System and Common Language Specification in .NET. It explains that the CTS defines a common type system for all languages with all types based on classes in the Framework Class Library. The CLS specifies a subset of the CTS that is supported across all languages to ensure interoperability. The document also provides examples of value types, reference types, boxing and unboxing in C#.
The document discusses the Common Type System and Common Language Specification in .NET. It explains that the CTS defines a common type system for all languages with all types based on classes in the Framework Class Library. The CLS specifies a subset of the CTS that is supported across all languages to ensure interoperability. The document also provides examples of value types, reference types, boxing and unboxing in C#.
2003 J oeHummel 1 C# Data Types CTS .NET is designed around the CTS, or Common Type System. allows assemblies, written in different languages, to work together To ensure interoperability across languages, Microsoft has also defined the CLS, or Common Language Specification 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 (CTS) CTS denotes one type system for all languages every type is based on a class in the FCL (i.e. fully-OOP!) all types inherit from Obj ect St r i ng Ar r ay Val ueType Except i on Del egat e Cl ass1 Mul t i cast Del egat e Cl ass2 Cl ass3 Obj ect Enum1 St r uct ur e1 Enum Pr 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 The Common Language Specification (CLS) Not all languages support all CTS types and features C#supports unsigned integer types, VB.NET does not C#is case sensitive, VB.NET is not C#supports pointer types (in unsafe mode), VB.NET does not C#supports operator overloading, VB.NET does not CLS was drafted to promote language interoperability vast majority of classes within FCL are CLS-compliant Mapping C# to CTS Language keywords map to common CTS classes: "hello", @"C:\dir\file.txt" character sequence string M or m suffix no suffix F or f suffix U/u and L/l suffix L or l suffix U suffix none none none none none 'A' '\x0041' '\u0041' true false Special format for literals Description Keyword 128 bit high precision decimal 64 bit signed integer long 64 bit floating point double 16 bit Unicode character char 32 bit floating point float 64 bit unsigned integer ulong 32 bit unsigned integer uint 32 bit signed integer int 16 bit unsigned integer ushort 16 bit signed integer short 8 bit unsigned integer byte 8 bit signed integer sbyte Boolean bool Overview: Modern Software Development Workshop Summer 2003 2003 J oeHummel 2 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; ... } } declarations decl +initializer error, x not set Type conversion Some type conversi ons are automatic from smaller to larger types Otherwise you need a cast or an explicit conversion type-cast syntax is type name inside parentheses conversions based on FCL classes int i = 5; double d = 3.2; string s = "496"; d = i; i = (int) d; i = int.Parse(s); j = System.Int32.Parse(s); k = System.Convert.ToInt32(d); implicit conversion typecast required conversion required Value vs. reference types .NET separates data types into two categories Value types: variable represents a value ("bits") Reference types: variable represents a reference to a heap-based object actual data resides in the object int i; i = 10; 10 string s; s = "calico"; "calico" How do you know which types are which? Types that inherit from Val ueType are values All other types are references Examples: let's work through the following int i; string s; double d; int[] a; i = 10; s = "calico"; d = 3.14159; a = new int[100]; Boxing and Unboxing When necessary, C# will auto-convert value <==> object 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! Classes Classes yield user-defined reference types Example: 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; } } Overview: Modern Software Development Workshop Summer 2003 2003 J oeHummel 3 Creating objects Objects are created using the Newoperator strings are a special case and don't require the use of new Customer c1, c2; string s1, s2; c1 = new Customer("jim bag", 36259); c2 = new Customer("jane doe", 55298); s1 = "an apple a day"; s2 = "keeps the doctor away"; Working with reference types Creating, assigning, and comparing let's work through the following Customer c1, c2, c3; string s1, s2; c1 = new Customer("jim bag", 36259); c2 = new Customer("jane doe", 55298); c3 = null; // c3 references no object c3 = c1; // c3 references same obj as c1 if (c1 == null) ... // does c1 ref an object? if (c1 == c2) ... // compares references if (c1.Equals(c2)) ... // compares objects if (s1 == s2) ... // exception: == overloaded to s1.Equals(s2) Defining equality Classes should override Equals Example: Customers are equal if their IDs are the same 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; // type-cast to access id return this.ID == other.ID; // equal if same id... } GetHashCode If you override Equals, must also override GetHashCode In .NET, GetHashCode is a cheap test for equivalence obj1.GetHashCode() !=obj2.GetHashCode() ==>not equal obj1.GetHashCode() ==obj2.GetHashCode() ==>unknown public class Customer { . . . public override int GetHashCode() { // delegate hash code computation to underlying integer class return this.ID.GetHashCode(); } 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; element access create number of elements Arrays of value types Value types yield preallocated data within array itself what does the situation look like in memory? int[] A1; A1 = new int[10]; A1[0] = 99; Point[] A2; A2 = new Point[10]; A2[0].x = 100; A2[0].y = 100; int[] A1; A1 = new int[10]; A1[0] = 99; Point[] A2; A2 = new Point[10]; A2[0].x = 100; A2[0].y = 100; public struct Point { public int x; public int y; } public struct Point { public int x; public int y; } Overview: Modern Software Development Workshop Summer 2003 2003 J oeHummel 4 Arrays of reference types Reference types yield references to objects outside array now what does the situation look like in memory? string[] A1; A1 = new string[10]; A1[0] = "apple"; Point[] A2; A2 = new Point[10]; A2[0] = new Point(); A2[0].x = 100; A2[0].y = 100; string[] A1; A1 = new string[10]; A1[0] = "apple"; Point[] A2; A2 = new Point[10]; A2[0] = new Point(); A2[0].x = 100; A2[0].y = 100; public class Point { public int x; public int y; } public class Point { public int x; public int y; } Multi-dimensional arrays C# supports arrays as a single object OR array of arrays latter allows you to implement jagged arrays 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; Summary CTS is the common type system same type system for all languages every type represented by underlying class in FCL fundamental difference between value & reference types CLS is the common language specification types that are guaranteed to work across languages Classes, Methods and Statements The class is key In .NET, classes play a central role every type is represented by a class all data and code must reside within a class public class App { public static void Main() { . . . } } public class Customer { . . . } public class Globals { . . . } public class Utility { . . . } Namespaces The FCL contains thousands of classes Inevitably, name collisions will occur between classes in the FCL between your classes and those in the FCL Namespaces are a way to minimize collisions as well as logically organize our code Overview: Modern Software Development Workshop Summer 2003 2003 J oeHummel 5 Definition A namespace N is a set of names qualified by N namespace Workshop { public class Customer { . . . } public class Product { . . . } }//namespace Workshop.Customer Workshop.Product Example Framework Class Library (FCL) contains 1000's of classes organized by namespaces FCL namespaces FCL's outermost namespace is " System" FCL technologies nested within System System.Xml.dll XML processing System.XML System.Windows.Forms.dll GUI System.Windows.Forms System.Data.dll Database access System.Data mscorlib.dll Data structures System.Collections mscorlib.dll Core classes, types System Assembl y Purpose Namespace Namespace != Assembly Orthogonal concepts: namespace for logical organization assembly for physical packaging You must reference an assembl y in order to use it You can " import" a namespace to reduce typing Fully-qualified references A fully-qualified reference starts with the outermost namespace: If you want, you can import a namespace & drop imported prefix using directive allows you to import a namespace System.Console.WriteLine("message"); using System; . . . Console.WriteLine("message"); Complete example using directive(s) specified at top of file /* main.cs */ using System; using Workshop; public class App { public static void Main() { Customer c; c = new Customer("jim bag", 94652); Console.WriteLine( c.ToString() ); } } namespace Workshop { public class Customer { . . . } public class Product { . . . } } Overview: Modern Software Development Workshop Summer 2003 2003 J oeHummel 6 Point of clarification using directive only includes types from specified namespace nested namespaces must be separately imported... /* main.cs */ using System; using System.Windows; using System.Windows.Forms; using System.Data; using System.Data.OleDb; public class App { . . . } Types of methods Classes contain 2 types of methods: subroutines with no return value (voi d) functions with a return value (i nt , st r i ng, etc.) Methods may be: instance static Instance methods require an object to call Static methods are global and thus require only class name Example Array class in FCL fully-qualified name is System.Array namespace System { public class Array { public int GetUpperBound(int dimension) { ... } public static void Sort(Array a) { ... } . . . } } instance method (absence of static) static method (presence of static) Calling methods Here's an example of calling into the Array class: /* main.cs */ using System; public class App { public static void Main() { int[] data = { 11, 7, 38, 55, 3 }; Array.Sort(data); for (int i=0; i<=data.GetUpperBound(0); i++) Console.WriteLine(i + ": " + data[i]); } } Parameter passing C# offers three options: pass-by-value (default) pass-by-reference pass-by-result ("copy-out") More subtle than you might think Case 1: pass-by-value with a value type Bits are copied public class App { public static void Main() { int i = 99; Foo(i); System.Console.WriteLine(i); // i = 99 } private static void Foo(int value) { value = value + 1; } } Stack stack frame for Foo val ue 99 i 99 stack frame for Main Overview: Modern Software Development Workshop Summer 2003 2003 J oeHummel 7 public class App { public static void Main() { int i = 99; Foo(ref i); System.Console.WriteLine(i); // i = 100 } private static void Foo(ref int value) { value = value + 1; } } Case 2: pass-by-ref with a value type Reference is passed Stack i 99 stack frame for Main stack frame for Foo val ue Case 3: pass-by-value with a reference type Reference is copied public class App { public static void Main() { int[] Vals; Vals = new int[1000]; Vals[0] = 99; Foo2(Vals); System.Console.WriteLine(Vals[0]); // 100 } private static void Foo2(int[] A) { A[0] = A[0] + 1; } } Stack Val s array A public class App { public static void Main() { int[] Vals; Vals = new int[1000]; Vals[0] = 99; Foo2(ref Vals); System.Console.WriteLine(Vals[0]); // 100 } private static void Foo2(ref int[] A) { A[0] = A[0] + 1; } } Case 4: pass-by-ref with a reference type Reference to reference is passed Stack Val s array A Case 5: pass-by-result? Pass-by-result is identical to pass-by-ref, except: no value is passed in result is copied back upon method return public class App { public static void Main() { int a, b; ComputeXYZ(out a, out b); System.Console.WriteLine("Results: " + a + ", " + b); } private static void ComputeXYZ(out int r1, out int r2) { r1 = ...; r2 = ...; } } Statements in C# C# supports the standard assortment Assignment Subroutine and function call Conditional if, switch Iteration for, while, do-while Control Flow return, break, continue, goto Examples x = obj.foo(); if (x > 0 && x < 10) count++; else if (x == -1) ... else { ... } while (x > 0) { ... x--; } for (int k = 0; k < 10; k++) { ... } Overview: Modern Software Development Workshop Summer 2003 2003 J oeHummel 8 foreach Specialized foreach loop provided for collections like array reduces risk of indexing error provides read only access int[] data = { 1, 2, 3, 4, 5 }; int sum = 0; foreach (int x in data) { sum += x; } foreach type value collection Summary Standard OOP language support: namespaces classes methods statements Two types of methods instance methods require an object to call static methods are global and thus require only class name
[Ebooks PDF] download Reading the Arab World A Content Based Textbook for Intermediate to Advanced Learners of Arabic 1st Edition Yehia A Mohamed full chapters
[Ebooks PDF] download Reading the Arab World A Content Based Textbook for Intermediate to Advanced Learners of Arabic 1st Edition Yehia A Mohamed full chapters