Generic trong C | 85 bài học lập trình C hay nhất PDF

7 99 0
Generic trong C  | 85 bài học lập trình C  hay nhất PDF

Đang tải... (xem toàn văn)

Thông tin tài liệu

http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Generic C# Generic C# cho phép bạn trì hỗn đặc điểm kỹ thuật (Specification) kiểu liệu việc lập trình phần tử lớp phương thức, tới thực sử dụng chương trình Nói cách khác, Generic cho phép bạn viết lớp phương thức mà làm việc với kiểu liệu Hiểu nôm na, đơn giản generic kiểu liệu C#, int, float, string, bool, , điểm khác biệt đây, generic kiểu liệu "tự do", nghĩa kiểu liệu được, tùy vào mục đích sử dụng, kiểu đại diện cho tất kiểu liệu lại Bạn viết specification cho lớp phương thức, với tham số thay cho kiểu liệu Khi Compiler bắt gặp constructor cho lớp lời gọi hàm cho phương thức, tạo code để xử lý kiểu liệu cụ thể Dưới ví dụ đơn giản giúp bạn hiểu khái niệm using System; using System.Collections.Generic; namespace GenericApplication { public class MyGenericArray { private T[] array; public MyGenericArray(int size) { array = new T[size + 1]; } public T getItem(int index) { return array[index]; } http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com public void setItem(int index, T value) { array[index] = value; } } class Tester { static void Main(string[] args) { //declaring an int array MyGenericArray intArray = new MyGenericArray(5); //setting values for (int c = 0; c < 5; c++) { intArray.setItem(c, c*5); } //retrieving the values for (int c = 0; c < 5; c++) { Console.Write(intArray.getItem(c) + " "); } Console.WriteLine(); //declaring a character array MyGenericArray charArray = new MyGenericArray(5); //setting values for (int c = 0; c < 5; c++) http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com { charArray.setItem(c, (char)(c+97)); } //retrieving the values for (int c = 0; c< 5; c++) { Console.Write(charArray.getItem(c) + " "); } Console.WriteLine(); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: 10 15 20 a b c d e Đặc điểm Generic C# Generic kỹ thuật làm cho chương trình C# bạn phong phú theo cách sau:  Nó giúp bạn tối đa hóa việc tái sử dụng code, an tồn kiểu, hiệu  Bạn tạo lớp generic collection Thư viện lớp Net Framework chứa số lớp generic collection System.Collections.Generic namespace Bạn sử dụng lớp generic collection thay cho lớp collection trongSystem.Collections namespace  Bạn tạo cho riêng Class, Interface, phương thức, Event Delegate dạng generic  Bạn tạo lớp generic mà cho bạn khả truy cập tới phương thức kiểu liệu cụ thể http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp  Copyright © vietjack.com Bạn lấy thông tin kiểu sử dụng kiểu liệu generic runtime phương thức Reflection Các phương thức Generic C# Trong ví dụ trước, sử dụng lớp generic, tương tự, khai báo phương thức generic với tham số kiểu Ví dụ sau minh họa điều này: using System; using System.Collections.Generic; namespace GenericMethodAppl { class Program { static void Swap(ref T lhs, ref T rhs) { T temp; temp = lhs; lhs = rhs; rhs = temp; } static void Main(string[] args) { int a, b; char c, d; a = 10; b = 20; c = 'I'; d = 'V'; //display values before swap: Console.WriteLine("Int values before calling swap:"); Console.WriteLine("a = {0}, b = {1}", a, b); Console.WriteLine("Char values before calling swap:"); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Console.WriteLine("c = {0}, d = {1}", c, d); //call swap Swap(ref a, ref b); Swap(ref c, ref d); //display values after swap: Console.WriteLine("Int values after calling swap:"); Console.WriteLine("a = {0}, b = {1}", a, b); Console.WriteLine("Char values after calling swap:"); Console.WriteLine("c = {0}, d = {1}", c, d); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: Int values before calling swap: a = 10, b = 20 Char values before calling swap: c = I, d = V Int values after calling swap: a = 20, b = 10 Char values after calling swap: c = V, d = I Generic Delegate C# Trong C#, bạn định nghĩa Generic Delegate với tham số kiểu Ví dụ: delegate T NumberChanger(T n); Ví dụ sau minh họa cách sử dụng generic delegate C#: using System; using System.Collections.Generic; http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com delegate T NumberChanger(T n); namespace GenericDelegateAppl { class TestDelegate { static int num = 10; public static int AddNum(int p) { num += p; return num; } public static int MultNum(int q) { num *= q; return num; } public static int getNum() { return num; } static void Main(string[] args) { //create delegate instances NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger nc2 = new NumberChanger(MultNum); //calling the methods using the delegate objects nc1(25); Console.WriteLine("Value of Num: {0}", getNum()); nc2(5); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: Value of Num: 35 Value of Num: 175 http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... collection System.Collections .Generic namespace Bạn sử dụng lớp generic collection thay cho lớp collection trongSystem.Collections namespace  Bạn tạo cho riêng Class, Interface, phương th c, Event Delegate... Console.Write(charArray.getItem (c) + " "); } Console.WriteLine(); Console.ReadKey(); } } } Khi code biên dịch th c thi, cho kết quả: 10 15 20 a b c d e Đ c điểm Generic C# Generic kỹ thuật làm cho chương trình. .. vietjack.com Bạn lấy thơng tin kiểu sử dụng kiểu liệu generic runtime phương th c Reflection C c phương th c Generic C# Trong ví dụ trư c, sử dụng lớp generic, tương tự, khai báo phương th c generic

Ngày đăng: 02/12/2017, 19:59

Mục lục

    Đặc điểm của Generic trong C#

    Các phương thức Generic trong C#

    Generic Delegate trong C#

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan