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

expert

Explain what is Weak Reference in C#?

junior

In how many ways you can pass parameters to a method?

expert

What is jagged array in C# and when to prefer jagged arrays over multi-dimensional arrays?

Bình luận

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

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