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

What is a stati...

Câu trả lời

A static constructor in C# is a special type of constructor that is used to initialize static data members of a class. It is called automatically before the first instance of the class is created or any static members are referenced. Here are the key points about static constructors in C#:

Characteristics of Static Constructors

  1. Static constructors have no access modifiers and cannot have parameters.
  2. They are used to initialize static data members of a class.
  3. They are called automatically before the first instance of the class is created or any static members are referenced.
  4. If a class has both instance and static constructors, the static constructor is called before the instance constructor.
  5. Static constructors cannot access instance members directly and cannot be called explicitly.

Example of a Static Constructor

csharp Copy
public class MyClass
{
    private static int count;

    static MyClass()
    {
        count = 0;
        Console.WriteLine("Static constructor called.");
    }

    public MyClass()
    {
        count++...
senior

senior

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

junior

What is Serialization?

senior

Can you create a function in C# which can accept varying number of arguments?

expert

Could you explain the difference between destructor , dispose and finalize method?

Bình luận

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

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