Sử dụng method Generic, class Generic trong C#
Kiểu dữ liệu generic là kiểu dữ liệu có thể tạo ra các thành phần tái sử dụng tránh việc duplicate code, cho phép bạn định nghĩa 1 hàm 1 lớp mà không cần chỉ ra đối số kiểu dữ liệu là gì.
- Sử dụng generic type tối đa hóa việc tái sử dụng code, đảm bảo và an toàn về kiểu dữ liệu, hiệu suất cao.
- Net Framwork hỗ trợ một số generic collection, chúng ta nên sử dụng bất kể khi nào có thể, thay vì dùng classes như ArrayList.
- Bạn có thể tạo generic cho riêng mình với nhiều loại khác nhau như: Interfaces, classes, methods, events và delegates.
- Generic classes có thể được ràng buộc để cho phép truy cập các methods trên các data riêng biệt.
Cùng xem các loại generic và ví dụ dưới đây:
Generic Type Parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Hondanho { | |
public class Program { | |
static void Swap<T>(ref T a, ref T b) { | |
T temp = a; | |
a = b; | |
b = temp; | |
} | |
public static void Main(String[] args) { | |
int a = 1, b = 2; | |
double c = 1.2, d = 2.2; | |
Swap(ref a, ref b); | |
Swap(ref c, ref d); | |
Console.WriteLine(a + ", " + b); | |
Console.WriteLine(c + ", " + d); | |
} | |
} | |
} |
Generic Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Hondanho { | |
public class ListExtension<T> { | |
public T[] Data { get; set; } | |
public ListExtension(int length) { | |
Data = new T[length]; | |
} | |
public void SetItem(int index, T value) { | |
if(index >= Data.Length || index < 0) throw new Exception(); | |
Data[index] = value; | |
} | |
public T GetItem(int index) { | |
if(index >= Data.Length || index < 0) throw new Exception(); | |
return Data[index]; | |
} | |
} | |
public class Program { | |
public static void Main(String[] args) { | |
var listData = new ListExtension<int>(10); | |
listData.SetItem(3, 5); | |
Console.WriteLine(listData.GetItem(3)); | |
} | |
} | |
} |
Generic Interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Hondanho { | |
public class IRepository<T> where T: Entity{ | |
Task<T> GetById(string id); | |
Task<List<T>>> GetAll(); | |
} | |
public class Repository<T> : IRepository<T> { | |
public Repository() {} | |
public async Task<T> GetById(string id) { | |
... | |
return await T; | |
} | |
public async Task<List<T>> GetAll() { | |
return await context.DbSet<T>.ToListAsync(); | |
} | |
} | |
public class Program { | |
public static async void Main(String[] args) { | |
var stuRepository = new BaseRepository<Student>(); | |
var listAllStudent = await stuRepository.GetAll(); | |
Console.WriteLine(listData.ToString()); | |
} | |
} | |
} |