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

Why doesn't C# allow static methods to implement an interface?

junior

What are the different types of classes 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