Danh mục

Chương 6: Array, Collection Types, and Iterators

Số trang: 49      Loại file: ppt      Dung lượng: 519.50 KB      Lượt xem: 19      Lượt tải: 0    
Thu Hiền

Hỗ trợ phí lưu trữ khi tải xuống: 9,000 VND Tải xuống file đầy đủ (49 trang) 0
Xem trước 5 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

This chapter covers the various array and collection typesavailable in C#. You can create two types ofmultidimensional arrays, as well as your own collectiontypes while utilizing collection-utility classes.
Nội dung trích xuất từ tài liệu:
Chương 6: Array, Collection Types, and IteratorsChapter 6. Arrays, Collection Types, and Iterators Hoang Anh Viet VietHA@it-hut.edu.vnHaNoi University of Technology 1 Objectives “This chapter covers the various array and collection types available in C#. You can create two types of multidimensional arrays, as well as your own collection types while utilizing collection-utility classes. You’ll see how to define forward, reverse, and bidirectional iterators using the new iterator syntax introduced in C# 2.0, so that your collection types will work well with foreach statements.”Microsoft 2 Roadmap 6.1. Introduction to Arrays  6.2. Multidimentional Rectangular Arrays  6.3. Multidimentional Jagged Arrays  6.4. Collection Types  6.5. Iterators  6.6. Collection Initializers Microsoft 3 6.1. Introduction to Arrays Overview  Implicitly Typed Arrays  Type Convertibility and Covariance  Arrays As Parameters (and Return Values)  The System.Array Base Class Microsoft 4 Overview An array is a set of data items, accessed using an numerical index  An array is a group of contiguous memory locations that all have  the same name and type Arrays are reference types, and  static void SimpleArrays() the memory for the array is { allocated on the managed heap. Console.WriteLine(=> Simple Array Creation.); // Create and fill an array of 3 Integers Array Initialization Syntax:  int[] myInts = new int[3]; myInts[0] = 100; Example:  myInts[1] = 200; 100 myInts[0] myInts[2] = 300; // Now print each value.Position number of the elementwithin array myInts foreach(int i in myInts) 200 myInts[1] Console.WriteLine(i); Console.WriteLine(); 300 } myInts[2] Microsoft 5 Figure 6-1: Array of reference types versus value typesMicrosoft 6 Implicitly Typed Arrays using System; public class EntryPoint C# 3.0 introduces an  { abbreviated way of initializing static void Main() arrays when the type of the { array can be inferred at // A conventional array int[] conventionalArray = new int[] { 1, 2, 3 }; runtime // An implicitly typed array When the compiler is  var implicitlyTypedArray = new [] { 4, 5, 6 }; presented with multiple types Console.WriteLine( implicitlyTypedArray.GetType() ); within the initialization list of an // An array of doubles implicitly typed array, it var someNumbers = new [] { 3.1415, 1, 6 }; Console.WriteLine( someNumbers.GetType() ); determines a type that all the // Won’t compile! given types are implicitly // var someStrings = new [] { int, convertible to. // someNumbers.GetType() }; Example:  } }Microsoft 7 Type Convertibility and Covariance using System; public class Animal { } When declaring an array to  public class Dog : Animal { } contain instances of a certain public class Cat : Animal { } type, the instances that may public class EntryPoint place in that array can actually Dog and Cat are { type-convertible to be instances of a more derived Animal static void Main() type. { Arrays are covariant  Dog[] dogs = new Dog[3]; Example: Cat[] cats = new Cat[2];  Animal[] animals = dogs; Animal[] moreAnimals = cats; } }Microsoft ...

Tài liệu được xem nhiều:

Tài liệu liên quan: