Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 49 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
49
Dung lượng
519,5 KB
Nội dung
Hoang Anh Viet VietHA@it-hut.edu.vn HaNoi University of Technology Chapter 6. Arrays, Collection Types, and Iterators 1 Microsoft 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.” 22 Microsoft 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 3 Microsoft 6.1. Introduction to Arrays Overview Implicitly Typed Arrays Type Convertibility and Covariance Arrays As Parameters (and Return Values) The System.Array Base Class 4 Microsoft 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 the memory for the array is allocated on the managed heap. Array Initialization Syntax: Example: 100 200 300 myInts[0] myInts[1] myInts[2] Position number of the element within array myInts static void SimpleArrays() { Console.WriteLine("=> Simple Array Creation."); // Create and fill an array of 3 Integers int[] myInts = new int[3]; myInts[0] = 100; myInts[1] = 200; myInts[2] = 300; // Now print each value. foreach(int i in myInts) Console.WriteLine(i); Console.WriteLine(); } 5 Microsoft Figure 6-1: Array of reference types versus value types 6 Microsoft Implicitly Typed Arrays C# 3.0 introduces an abbreviated way of initializing arrays when the type of the array can be inferred at runtime When the compiler is presented with multiple types within the initialization list of an implicitly typed array, it determines a type that all the given types are implicitly convertible to. Example: using System; public class EntryPoint { static void Main() { // A conventional array int[] conventionalArray = new int[] { 1, 2, 3 }; // An implicitly typed array var implicitlyTypedArray = new [] { 4, 5, 6 }; Console.WriteLine( implicitlyTypedArray.GetType() ); // An array of doubles var someNumbers = new [] { 3.1415, 1, 6 }; Console.WriteLine( someNumbers.GetType() ); // Won’t compile! // var someStrings = new [] { "int", // someNumbers.GetType() }; } } 7 Microsoft Type Convertibility and Covariance When declaring an array to contain instances of a certain type, the instances that may place in that array can actually be instances of a more derived type. Arrays are covariant Example: using System; public class Animal { } public class Dog : Animal { } public class Cat : Animal { } public class EntryPoint { static void Main() { Dog[] dogs = new Dog[3]; Cat[] cats = new Cat[2]; Animal[] animals = dogs; Animal[] moreAnimals = cats; } } Dog and Cat are type-convertible to Animal 8 Microsoft Arrays As Parameters (and Return Values) Once you have created an array, you are free to pass it as a parameter and receive it as a member return value static void PrintArray(int[] myInts) { for(int i = 0; i < myInts.Length; i++) Console.WriteLine("Item {0} is {1}", i, myInts[i]); } static string[] GetStringArray() { string[] theStrings = { "Hello", "from", "GetStringArray" }; return theStrings; } static void PassAndReceiveArrays() { Console.WriteLine("=>Arrays as params and return values."); . int[] ages = {20, 22, 23, 0} ; PrintArray(ages); string[] strs = GetStringArray(); foreach(string s in strs) Console.WriteLine(s); Console.WriteLine(); } Pass array as parameter Get array as return value. 9 Microsoft The System.Array Base Class The System.Array type houses the fundamental methods and properties that are essential to an array Some members: Clear() : set a range of elements in the array to empty values. CopyTo() : copy elements from the source array into the destination array Length : return the number of items Rank : return the number of dimensions Reverse() : reverse the contents of a one-dimensional array Sort() : sort a one-dimensional array fo intrinsic types Example: 10 [...]... 6.4 Collection Types 6.5 Iterators 6.6 Collection Initializers Microsoft 19 6.4 Collection Types Ever since its inception, the NET Framework has offered a host of collection types for managing everything from an expandable array via ArrayList, a Queue, a Stack, or even a dictionary via the HashTable class Over the years, newer version of the NET Framework expanded these types Generally, a collection. .. objects and implements IEnumerable or IEnumerable The objects in the set are typically related to each other in some way defined by the problem domain Microsoft 20 6.4 Collection Types(2) The Interface of the System.Collections Namespace The Class Types of System.Collections The Sytem.Collections.Generic Namespace Microsoft 21 The Interface of the System.Collections Namespace The System.Collections... Microsoft public interface ICollection : IEnumerable { int Count { get; } bool IsSynchronized { get; } object SyncRoot { get; } void CopyTo(Array array, int index); } 24 The Role of IDictionary A dictionary is a collection that maintains a set of name/value pairs Defines a Keys and Values property and Add(), Remove(), Contains() methods public interface IDictionary : ICollection, IEnumerable Definition:... array"); // and print them for (int i = 0; i . Viet VietHA@it-hut.edu.vn HaNoi University of Technology Chapter 6. Arrays, Collection Types, and Iterators 1 Microsoft Objectives “This chapter covers the various array and collection types available in C#. You can create. Arrays 6.4. Collection Types 6.5. Iterators 6.6. Collection Initializers 19 Microsoft 6.4. Collection Types Ever since its inception, the .NET Framework has offered a host of collection. Arrays 6.4. Collection Types 6.5. Iterators 6.6. Collection Initializers 3 Microsoft 6.1. Introduction to Arrays Overview Implicitly Typed Arrays Type Convertibility and Covariance Arrays