Reprinted from Microsoft related official documents;
Https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/using-constructors
Whenever a class or struct is created, its constructor is called. a class or struct may have multiple constructors that take different parameters. using constructors, programmers can set default values, restrict instantiation, and write code that is flexible and readable.
The constructor has the same name as the class or struct and typically initializes the data members of the new object. Its method signature contains only the method name and its argument list; it does not contain a return type.
Public class person{ privatestring last ; Private string First ; Public Person (stringstring firstName) { = lastName; = firstName; } // Remaining implementation of person class.}
Or
Public class location{ privatestring locationname; Public Location (string name) = = LocationName = name ; Public string Name { get = = LocationName ; set = = LocationName = value;} }
Using constructors
The class of the Taxi . new operator immediately calls Taxi constructors.
Public class taxi{ publicbool isinitialized; Public Taxi () { true; }} class testtaxi{ staticvoid Main () { new Taxi (); Console.WriteLine (t.isinitialized); }}
S
1. You can prevent a class from being instantiated by setting the constructor to a private constructor--class NLog {private NLog () {} }
< The span data-ttu-id= "021c3-141" > constructor can be marked as public, private, protected, internal, protected internally, or privately protected. These access modifiers define how the class's users construct the class.
2. The constructor of a struct type is similar to a class's constructor, but structs cannot contain an explicit default constructor, because the compiler automatically provides an explicit default constructor.
3. structs can define constructors with parameters. new statement or base statement. structs can also define multiple constructors, and neither need to define a default constructor.
4. constructor can use the This keyword to invoke another constructor in the same object. base , This can be used with or without parameters, and any parameter in the constructor can be used as a this A parameter of the , or as part of an expression.
the base keyword can be used with parameters or without parameters. any argument to the constructor can be used as a parameter to base or as part of an expression.
E
default constructor
Constructors without any parameters are called "Default constructors". new The default constructor is called whenever an object is instantiated with an operator and is not new supplied with any arguments.
If you do not provide a constructor for a class, by default, C # creates a constructor that instantiates the object and sets the member variable to the default value. If you do not provide a constructor for the struct, C # will rely on the implicit default constructor to automatically initialize each field of the value type to its default value.
Static constructors
A class or struct can also have a static constructor that initializes a static member of the type. a static constructor is a parameterless constructor. if a static constructor is not provided to initialize a static field, the C # compiler provides a default static constructor
C # Foundation-constructors