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

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

middle

What are the uses of using in C#

expert

What's the difference between the System.Array.CopyTo() and System.Array.Clone() ?

middle

What is scope of a Internal member variable of a C# class?

Bình luận

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

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