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
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 ...
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ìm kiếm theo từ khóa liên quan:
ngôn ngữ lập trình C# học lập trình C# cấu trúc lập trình C tài liệu về lập trình C tính đa hình trong lập trình hướng đối tượngTài liệu liên quan:
-
Tóm tắt Đồ án tốt nghiệp Công nghệ thông tin: Lập trình game với ứng dụng Unity
16 trang 487 0 0 -
Tóm tắt Đồ án tốt nghiệp Công nghệ thông tin: Xây dựng game 2D trên Unity
21 trang 354 1 0 -
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 241 0 0 -
15 trang 200 0 0
-
Khóa luận tốt nghiệp: Xây dựng website dạy ngôn ngữ lập trình C# dành cho người mới bắt đầu
113 trang 65 0 0 -
20 trang 61 0 0
-
Bài giảng Cơ sở lập trình Csharp: Bài 1 - Làm quen với ngôn ngữ lập trình C#
36 trang 48 0 0 -
Tập bài giảng Lập trình trên nền Web
281 trang 42 0 0 -
16 trang 41 0 0
-
Luận văn tốt nghiệp Công nghệ thông tin: Quản lý hồ sơ Đảng viên trường Đại học Võ Trường Toản
72 trang 41 0 0