Trang chủ
Bài viết mới
Diễn đàn
Bài mới trên hồ sơ
Hoạt động mới nhất
VIDEO
Mùa Tết
Văn Học Trẻ
Văn Học News
Media
New media
New comments
Search media
Đại Học
Đại cương
Chuyên ngành
Triết học
Kinh tế
KHXH & NV
Công nghệ thông tin
Khoa học kĩ thuật
Luận văn, tiểu luận
Phổ Thông
Lớp 12
Ngữ văn 12
Lớp 11
Ngữ văn 11
Lớp 10
Ngữ văn 10
LỚP 9
Ngữ văn 9
Lớp 8
Ngữ văn 8
Lớp 7
Ngữ văn 7
Lớp 6
Ngữ văn 6
Tiểu học
Thành viên
Thành viên trực tuyến
Bài mới trên hồ sơ
Tìm trong hồ sơ cá nhân
Credits
Transactions
Xu: 0
Đăng nhập
Đăng ký
Có gì mới?
Tìm kiếm
Tìm kiếm
Chỉ tìm trong tiêu đề
Bởi:
Hoạt động mới nhất
Đăng ký
Menu
Đăng nhập
Đăng ký
Install the app
Cài đặt
Chào mừng Bạn tham gia Diễn Đàn VNKienThuc.com -
Định hướng Forum
Kiến Thức
- HÃY TẠO CHỦ ĐỀ KIẾN THỨC HỮU ÍCH VÀ CÙNG NHAU THẢO LUẬN Kết nối:
VNK X
-
VNK groups
| Nhà Tài Trợ:
BhnongFood X
-
Bhnong groups
-
Đặt mua Bánh Bhnong
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về Generic trong C#
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Trả lời chủ đề
Nội dung
<blockquote data-quote="Hoàng Xuân Bách" data-source="post: 181236" data-attributes="member: 315148"><p><span style="font-size: 18px"><strong>Các loại Generic</strong></span></p><p></p><p><span style="font-size: 18px"><strong>1 Generic Type Parameters</strong></span></p><p><span style="font-size: 18px">Trong ví dụ ở đầu bài viết thì ExampleList<T> không thực sự là một Generic Type Parameter, bởi vì muốn dùng nó chúng ta cần khởi tạo. Generic Type Parameter có thể hiểu nó như một kiểu dữ liệu có thể dùng khai báo biến, trong đó có một kiểu T là động, nghĩa là chúng ta có thể truyền nhiều kiểu dữ liệu khác nhau cho nó, ví dụ trong C# ta có những kiểu có thể dùng Generic Type Parameter như:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">public int IComparer<T>() { return 0; }</span></p><p><span style="font-size: 18px">public delegate bool Predicate<T>(T item);</span></p><p><span style="font-size: 18px">public struct Nullable<T> where T : struct { /*...*/ }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Đây chúng ta có thể gọi là Generic Type</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>2. Generic classes</strong></span></p><p><span style="font-size: 18px">Generic classes đóng gói các xử lý mà không chỉ định rõ kiểu dữ liệu. Hầu hết các trường hợp phổ biến sử dụng generic classes là với collections giống như: danh sách liên kết (Linked List), hash tables, queues, trees,..Các xử lý giống như thêm mới, gỡ bỏ item trong collection cơ bản được thực hiện theo cùng một cơ chế bất kể kiểu dữ nào được lưu trữ trong collection.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Khi tạo custom generic classes của riêng bạn, những điểm quan trọng cần xem xét đó là:</span></p><p><span style="font-size: 18px"></span></p><ul> <li data-xf-list-type="ul"><span style="font-size: 18px">Những types nào được khái quát hóa đến type parameters.<br /> Theo quy tắc, bạn càng tham số hóa càng nhiều, code của bạn càng mềm dẻo và dễ tái sử dụng. Tuy nhiên, điều đó cũng có thể làm code khó để đọc và hiểu cho người khác.</span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">Những ràng buộc gì để áp dụng type parameters</span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">Liệu implement một hoặc nhiều interfaces.<br /> Cho ví dụ, nếu bạn đang thiết kế một class mà sẽ sử dụng để tạo các items dựa trên collection, bạn có thể phải implement một interface giống như IComparable<T> where T</span></li> </ul><p><span style="font-size: 18px">Code bên dưới là một ví dụ về Generic class:</span></p><p><span style="font-size: 18px"></span></p><p> <span style="font-size: 18px">class Demo<T></span></p><p> <span style="font-size: 18px">{</span></p><p><span style="font-size: 18px"> T value;</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public Demo(T t)</span></p><p><span style="font-size: 18px"> {</span></p><p><span style="font-size: 18px"> value = t;</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public void Write()</span></p><p><span style="font-size: 18px"> {</span></p><p><span style="font-size: 18px"> Console.WriteLine(value);</span></p><p><span style="font-size: 18px"> }</span></p><p> <span style="font-size: 18px">}</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">class Program</span></p><p><span style="font-size: 18px">{</span></p><p><span style="font-size: 18px"> static void Main()</span></p><p><span style="font-size: 18px"> {</span></p><p><span style="font-size: 18px"> Demo<int> test1 = new Demo<int>(10);</span></p><p><span style="font-size: 18px"> test1.Write();</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> Demo<string> test2 = new Demo<string>("Cat");</span></p><p><span style="font-size: 18px"> test2.Write();</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> Console.ReadLine();</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px">}</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>3 Generic Interface</strong></span></p><p><span style="font-size: 18px">Nó thường hữa ích để định nghĩa cho collection classes, hoặc cho generic classes. Có thể lấy ví dụ như IComparable<T>.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Ví dụ sau demo cách sử dụng Generic interface. Giả sử ta xây dựng một chức năng getAll và save data của đối tượng Book và Author xuống DB. Chúng ta sẽ sử dụng một base interface và triển khai như bên dưới:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">class Book</span></p><p><span style="font-size: 18px">{</span></p><p><span style="font-size: 18px"> public string Name { get; set; }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public int Page { get; set; }</span></p><p><span style="font-size: 18px">}</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">class Author</span></p><p><span style="font-size: 18px">{</span></p><p><span style="font-size: 18px"> public string Name { get; set; }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public int Age { get; set; }</span></p><p><span style="font-size: 18px">}</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">interface IBaseRepository<T></span></p><p><span style="font-size: 18px">{</span></p><p><span style="font-size: 18px"> List<T> getAll();</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> T Save(T item);</span></p><p><span style="font-size: 18px">}</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">class BaseRepository<T>: IBaseRepository<T></span></p><p><span style="font-size: 18px">{</span></p><p><span style="font-size: 18px"> public List<T> getAll()</span></p><p><span style="font-size: 18px"> {</span></p><p><span style="font-size: 18px"> return new List<T>();</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public T Save(T item)</span></p><p><span style="font-size: 18px"> {</span></p><p><span style="font-size: 18px"> return item;</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px">}</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">class Program</span></p><p><span style="font-size: 18px">{</span></p><p><span style="font-size: 18px"> static void Main()</span></p><p><span style="font-size: 18px"> {</span></p><p><span style="font-size: 18px"> BaseRepository<Book> bookRepository = new BaseRepository<Book>();</span></p><p><span style="font-size: 18px"> BaseRepository<Author> authorRepository = new BaseRepository<Author>();</span></p><p><span style="font-size: 18px"> Book book = bookRepository.Save(new Book { Name = "Book1", Page = 100 });</span></p><p><span style="font-size: 18px"> Author author = authorRepository.Save(new Author { Name = "Author1", Age = 50 });</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> Console.WriteLine("Book: {0} - page: {1}", book.Name, book.Page);</span></p><p><span style="font-size: 18px"> Console.WriteLine("Name: {0} - age: {1}", author.Name, author.Age);</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> Console.ReadLine();</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px">}</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>4 Generic method</strong></span></p><p><span style="font-size: 18px">Một generic method được khai báo với type parameter, có thể hiểu là kiểu của tham số là dynamic (không được chỉ định trước cho đến khi gọi method đó) như code bện dưới:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">class Program</span></p><p><span style="font-size: 18px">{</span></p><p><span style="font-size: 18px"> static int Compare<T>(T first, T second)</span></p><p><span style="font-size: 18px"> {</span></p><p><span style="font-size: 18px"> if(first.Equals(second))</span></p><p><span style="font-size: 18px"> {</span></p><p><span style="font-size: 18px"> return 0;</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> return 1;</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> static void Main()</span></p><p><span style="font-size: 18px"> {</span></p><p><span style="font-size: 18px"> int result1 = Compare(2, 2);</span></p><p><span style="font-size: 18px"> int result2 = Compare("abc", "def");</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> Console.WriteLine(result1);</span></p><p><span style="font-size: 18px"> Console.WriteLine(result2);</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> Console.ReadLine();</span></p><p><span style="font-size: 18px"> }</span></p></blockquote><p></p>
[QUOTE="Hoàng Xuân Bách, post: 181236, member: 315148"] [SIZE=5][B]Các loại Generic[/B][/SIZE] [SIZE=5][B]1 Generic Type Parameters[/B] Trong ví dụ ở đầu bài viết thì ExampleList<T> không thực sự là một Generic Type Parameter, bởi vì muốn dùng nó chúng ta cần khởi tạo. Generic Type Parameter có thể hiểu nó như một kiểu dữ liệu có thể dùng khai báo biến, trong đó có một kiểu T là động, nghĩa là chúng ta có thể truyền nhiều kiểu dữ liệu khác nhau cho nó, ví dụ trong C# ta có những kiểu có thể dùng Generic Type Parameter như: public int IComparer<T>() { return 0; } public delegate bool Predicate<T>(T item); public struct Nullable<T> where T : struct { /*...*/ } Đây chúng ta có thể gọi là Generic Type [B]2. Generic classes[/B] Generic classes đóng gói các xử lý mà không chỉ định rõ kiểu dữ liệu. Hầu hết các trường hợp phổ biến sử dụng generic classes là với collections giống như: danh sách liên kết (Linked List), hash tables, queues, trees,..Các xử lý giống như thêm mới, gỡ bỏ item trong collection cơ bản được thực hiện theo cùng một cơ chế bất kể kiểu dữ nào được lưu trữ trong collection. Khi tạo custom generic classes của riêng bạn, những điểm quan trọng cần xem xét đó là: [/SIZE] [LIST] [*][SIZE=5]Những types nào được khái quát hóa đến type parameters. Theo quy tắc, bạn càng tham số hóa càng nhiều, code của bạn càng mềm dẻo và dễ tái sử dụng. Tuy nhiên, điều đó cũng có thể làm code khó để đọc và hiểu cho người khác.[/SIZE] [*][SIZE=5]Những ràng buộc gì để áp dụng type parameters[/SIZE] [*][SIZE=5]Liệu implement một hoặc nhiều interfaces. Cho ví dụ, nếu bạn đang thiết kế một class mà sẽ sử dụng để tạo các items dựa trên collection, bạn có thể phải implement một interface giống như IComparable<T> where T[/SIZE] [/LIST] [SIZE=5]Code bên dưới là một ví dụ về Generic class: class Demo<T> { T value; public Demo(T t) { value = t; } public void Write() { Console.WriteLine(value); } } class Program { static void Main() { Demo<int> test1 = new Demo<int>(10); test1.Write(); Demo<string> test2 = new Demo<string>("Cat"); test2.Write(); Console.ReadLine(); } } [B]3 Generic Interface[/B] Nó thường hữa ích để định nghĩa cho collection classes, hoặc cho generic classes. Có thể lấy ví dụ như IComparable<T>. Ví dụ sau demo cách sử dụng Generic interface. Giả sử ta xây dựng một chức năng getAll và save data của đối tượng Book và Author xuống DB. Chúng ta sẽ sử dụng một base interface và triển khai như bên dưới: class Book { public string Name { get; set; } public int Page { get; set; } } class Author { public string Name { get; set; } public int Age { get; set; } } interface IBaseRepository<T> { List<T> getAll(); T Save(T item); } class BaseRepository<T>: IBaseRepository<T> { public List<T> getAll() { return new List<T>(); } public T Save(T item) { return item; } } class Program { static void Main() { BaseRepository<Book> bookRepository = new BaseRepository<Book>(); BaseRepository<Author> authorRepository = new BaseRepository<Author>(); Book book = bookRepository.Save(new Book { Name = "Book1", Page = 100 }); Author author = authorRepository.Save(new Author { Name = "Author1", Age = 50 }); Console.WriteLine("Book: {0} - page: {1}", book.Name, book.Page); Console.WriteLine("Name: {0} - age: {1}", author.Name, author.Age); Console.ReadLine(); } } [B]4 Generic method[/B] Một generic method được khai báo với type parameter, có thể hiểu là kiểu của tham số là dynamic (không được chỉ định trước cho đến khi gọi method đó) như code bện dưới: class Program { static int Compare<T>(T first, T second) { if(first.Equals(second)) { return 0; } return 1; } static void Main() { int result1 = Compare(2, 2); int result2 = Compare("abc", "def"); Console.WriteLine(result1); Console.WriteLine(result2); Console.ReadLine(); }[/SIZE] [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về Generic trong C#
Top