C # _ instance constructors

Instance constructors
instance constructor is a special method, which executes when you create each new power class.
The constructor for the state to initialize instances of the class.
If you want to externally created instance of the class from the class, you need to constructor declared as public.
constructor:
same name and the name of the class constructor.
constructors can not return a value.
--------------------------- -------------------------------------------------- -----------------------
constructor arguments
constructor and other methods similar to the following aspects.
constructor can take parameters. parameter syntax and other methods identical.
constructors can be overloaded.
when used to create an object instance of the class to create a new expression, to use the new operator, followed by a class constructor .new operators use this constructor creates an instance of the class.
-------------------------------------------------- -------------------------------------------------
default constructors
if not explicitly provided in the class declaration instance constructor, the compiler provides an implicit default constructor, it The following characteristics:
no parameters.
The method body is empty.
If you declare any constructors for a class, the compiler will not be defined for the class default constructor.
-------------------------------------------------- -------------------------------------------------
static constructor
constructor can be declared as static instance of each new instance constructor to initialize the class, and static constructor to initialize the class-level items are typically static field static constructor to initialize the class...
to initialize the class level item:
in reference before any static member
. before any forces to create a class
static constructor and instance constructors similar to the following: the
name of a static constructor must be the class name same as
the constructor does not return a value.
static constructors in the following aspects and examples constructors different:
the static constructor declaration using the static keyword.
class can have a static constructor, and can not have parameters.
static constructor can not have access modifiers.
other important details about the static constructor should be aware of the following:
1 ). class can have either a static constructor may have an instance constructor.
2). as with the static method, a static member of a class instance constructors can not access the location, and therefore can not use this access.
3). not from Program explicitly call static constructor, the system will automatically call them at:
before any instance of the class is created;
any static member of the class is referenced before.

Guess you like

Origin www.cnblogs.com/linchenjian/p/11425278.html