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

Can this be used within a Static method?

Câu trả lời

Sure, I'd be happy to help you with that C# interview question. Here's the answer:

In C#, the keyword this is used to refer to the current instance of a class. It is typically used within instance methods and constructors to access instance members (fields, properties, events, and methods) of the current object.

However, this cannot be used directly within a static method. Static methods belong to the class itself, not to any specific instance of the class. Therefore, there is no current instance available to reference using this.

If you try to use this within a static method, you will get a compile-time error:

csharp Copy
public class MyClass
{
    public static void StaticMethod()
    {
        // Error: Use of unassigned local variable 'this'
        Console.WriteLine(this.ToString());
    }
}

To acces...

junior

junior

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

senior

How to solve Circular Reference?

junior

What is enum in C#?

senior

What is the difference between is and as operators in C#?

Bình luận

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

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