1/25/2008 Administration CMS student list populated C# Types Let me know if you have not received CMS emails First assignment released Mingsheng Hong Due on Feb. 1 CS 215, Spring 2008 Write a C# program that takes as input regular English text returns the transpose Use C# style much of what we discussed so far will be useful should be short Review .NET Based Frameworks Visual C# 2008 Express walkthrough ASP.NET: web application framework Use .NET languages in server-side web pages .NET framework, CLR, CTS, CLS Now supports AJAX! ADO.NET: Use the CLR to access databases in the manner of ODBC provides classes for access Microsoft XNA: game design framework GDIAC at http://gdiac.cis.cornell.edu CIS 300: Introduction to Computer Game Design Roadmap for Today’s Lecture Common Type System C# types Reference types Value types Boxing and unboxing Basic C# features Arrays OO features Function parameters Iterators From MSDN 1
1/25/2008 Common Value Types Common Reference Type string type: string Integer types: signed: sbyte, int, short, long can index like char array unsigned: byte, uint, ushort, ulong has methods such as Split Floating point: float, double, decimal e.g., Everything in C# inherits from object string s = “Hello”; char third = s[2]; regular objects are heavy-weight string[] split = s.Split(third); for efficiency, need primitive types Common Types Reference Types What is the difference between string and double? Single inheritance in class hierarchy, rooted reference types vs. value types in object two families of types in C# Simple to Java classes Implement arbitrarily many interfaces Similar to Java interfaces C# classes can be abstract must be explicitly marked as abstract (contrast with C++) May contain non-method non-data members Reference Types: Value Types: Memory Layout Memory Layout Refer to a memory location Contain the actual value, not the location very much like pointers in other languages like Inherit from System.ValueType C/C++ treated specially by the runtime: no subclassing Can be set to null Copies of value types make a real copy memory memory a { 137 a { A a = new A(); } int a = 137; var of class A A b = a; int b = a; 137 b } } b 2
1/25/2008 Boxing and Unboxing Differences between Types Copy semantics for assignment: Value types not objects MyClass a = new MyClass(); performance gain in common case MyClass b = a; but can become objects on demand b.X = 10; Console.WriteLine(a.X); //output ? called “boxing”. Reverse is “ unboxing ” int a = 1; Note: boxing still copies int b = a; b = 10; { memory Console.WriteLine(a); //output ? int a = 137; 137 a o1 object o1 = a; Important for parameter passing, too int 137 object o2 = o1; 137 b int b = (int)o2; o2 } Common Value Types Enum Example Simple types (i.e., integer and floating point) Definition enum Color Enum types { has a corresponding underlying type (default: int) Red, Green, Blue } Instantiation Color c = Color.Red; Common Value Types Struct Example Simple types (i.e., integer and floating point) Definition struct Point { Enum types public int x, y; has a corresponding underlying type (default: int) public Point(int x, int y) { this.x = x; Struct types this.y = y; user-defined value types } can contain arbitrary data } Instantiation non-extensible (sealed subclasses) Point a = new Point(10, 10); examples: Point, KeyValuePair a.x = 20; 3
1/25/2008 C# Variables Basic C# Features Definite assignment • Arrays int i; //i is a local variable • OO features Console.WriteLine(i); //error CS0165: Use • Accessibility of unassigned local variable 'i' Class members • Default values • Property only for instance variables, static variables, and array elts • Indexer eg. • Operator string s; // s == null double x; // x == 0.0 • Function parameters • Iterators C# Arrays C# Arrays Can have standard C arrays Multidimensional int[] array = new int[30]; stored sequentially int[][] array = new int[2][]; int[,] array = new int[10,30]; array[0] = new int[100]; array[3,7] = 137; array[1] = new int[1]; Called “jagged” arrays Stored in random parts of the heap Can have arbitrary dimensions Recall that an array is an object 4
Recommend
More recommend