Câu hỏi phỏng vấn C#
Câu hỏi

What are generics in C#?

Câu trả lời

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#:

Type Parameters

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:

csharp Copy
public class Box<T>
{
    private T item;

    public void Set(T item)
    {
        this.item = item;
    }

    public T Get()
    {
        return item;
    }
}

Code Reuse

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.

Type Safety

Generics pr...

junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What is the use of the IDisposable interface?

expert

What is a preprocessor directives in C#?

expert

What are the benefits of a Deferred Execution in LINQ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào