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

junior

Why to use finally block in C#?

senior

Is operator overloading supported in C#?

junior

What you understand by Value types and Reference types in .NET? Provide some comparison

Bình luận

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

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