What are generics in C#?
What are generics in C#?
Generics in C# are a feature that allows you to write code that works with different data types without needing to know the specific type in advance. They provide a way to create classes, methods, and collections that can handle different types of data without the need for explicit type casting or boxing/unboxing operations.
Here are some key points about generics in C#:
Generics use type parameters to represent the types that will be used in the generic code. These type parameters are enclosed in angle brackets (< >) and can be used in place of specific types throughout the generic code.
Example:
public class Box<T>
{
private T item;
public void Set(T item)
{
this.item = item;
}
public T Get()
{
return item;
}
}
Generics allow you to write code that can be reused with different data types. This promotes code reuse and reduces the amount of duplicate code needed to handle different types.
Generics pr...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào