C # static constructor and non-static constructors

It formed after the static constructor in front of the constructor method of adding the static keyword, and no modifier (public, private), no parameters.

Features:
1, modify the static constructor has no modifier (public, private), because the static constructor is not our programmers call, by .net framework calls at the right time.
2, static constructor has no parameters, because the framework is impossible to know what we need to add parameters in functions, so you can not use parameters specified.
3, in front of a static constructor must be static keyword. If you do not add this keyword, it is a general constructor.
4, the static constructor instance variables can not be instantiated. (Variables can be divided into the class and instance-level variables, class level have modified the static keyword).
5, the timing of the static function call is invoked when the class is instantiated or invoked static members, and .net framework by calling the static constructor to initialize a static member variable.
6, a class can have a static constructor.
7, no-argument constructor and a static constructor with no arguments can co-exist. Because they belong to a class level, a part of the instance level, not in conflict.
8, the static constructor is executed only once. And is called the timing of call features 5 in.
9. If you do not write a constructor in the class, then the framework will generate a constructor for us, so if we define a static variable in the class, but it does not define a static constructor, then the framework will help us to generate a static The constructor to call to make the frame itself.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class A
    {
        static A()
        {
            Console.WriteLine("1");
        }
        public A()
        {
            Console.WriteLine("2");

        }
    }
    class B:A
    {
        static B()
        {
            Console.WriteLine("a");
        }
        public B()
        {
            Console.WriteLine("b");

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
           //a 1 2 b
            A ab = new B();
            Console.WriteLine("--------");
            //2 b
            ab = new B();
            Console.ReadKey();
        }
    }
}

Results of the:

 

1、A ab = new B();

Examples of A. B, execution of static constructors == B output `A `

B. Implementation of the constructor of B, since B inherits from A, the first into the A

C. Examples of A, performing static constructor A, the output == `1 '

D. Implementation A constructor output == '2 '

E. Finally, the constructor of B == Back output `b`

2、ab = new B();

Because the static constructor creates only once, so they will not enter the static constructor 

A. Because B inherits from A, the first into the constructor of A == output `2 '

B. re-entering the constructor of B == output `b`


Original link: https://blog.csdn.net/hyunbar/article/details/81171335

 

Guess you like

Origin www.cnblogs.com/wintertone/p/11513085.html