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

Can you add extension methods...

Câu trả lời

Yes, you can add extension methods to an existing static class in C#. Here's how:

Adding Extension Methods to a Static Class

  1. Create a static class to hold your extension methods.
  2. Add the this keyword before the first parameter of the extension method, specifying the type you want to extend.
  3. The first parameter of the extension method becomes the object the method is called on.

Here's an example:

csharp Copy
public static class StringExtensions
{
    public static string Reverse(this string str)
    {
        return new string(str.Reverse().ToArray());
    }
}

In this example:

  • We create a static class called StringExtensions.
  • We add an extension method called Reverse that takes a string as its first parameter, preceded by the this keyword.
  • Inside the...
expert

expert

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

expert

What are Circular References in C#?

senior

What is Marshalling and why do we need it?

senior

What is the difference between System.ApplicationException class and System.SystemException class?

Bình luận

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

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