C # basic knowledge memo (2)

Source: Internet
Author: User

Constructor

The execution sequence of the constructor is to execute the constructor of the base class before executing the constructor of the subclass. In this way, sub-class constructor can call accumulated methods, attributes, and members. If the base class constructor is declared as private, the subclass cannot be constructed.

If a constructor with parameters is provided, the compiler does not automatically provide default constructor. The Compiler only provides default constructor if no constructor is defined. If a constructor with parameters is provided, the compiler considers it a unique constructor and does not implicitly provide other constructor functions.

Static constructor: the object class has some static fields or attributes and needs to be initialized before the first time the class is used.

    • No parameter;
    • InCodeThe referenced class is previously executed only once. It is usually executed before the first member that can be called for the class;
    • There are no definite execution opportunities and no sequence.
    • Other C # code does not access static constructor, And the. NET Runtime Library calls it. So public or protect is meaningless.
    • A class can have only one static constructor.
    • Static functions can only access Fan Wen static members, but cannot access instance members.

This calls only the constructors that can most match the parameters.

Read-Only Field

Its value should not be changed, but it is unknown before running. The read-only field value can be determined through some operations. Only read-only fields can be assigned values in the constructor.

The read-only field can be an instance field rather than a static field. each instance of such a class can have different values. To set the read-only field to static, the declaration must be displayed. You do not need to set the read-only fields to private, because they cannot be modified externally as defined.

Structure
Struct route
{
Public orientation dir;
Public double distance;
}

The structure does not support inheritance, but can inherit interfaces.

The compiler always provides a non-parametric default constructor for the structure and cannot be replaced.

After the structure declaration, you can directly call the data member initialization without the need for new. When the structure is used as a parameter, use ref to avoid performance loss.

Structure Inheritance and system. valuetype, system. valuetype inherit from system. object, so the structure can access the system. Object method.

New & override

The new keyword has no relationship with inheritance and can be used as a subclass of a class. When a subclass method uses new (this method can be the same name or different from a method of the base class), the same name method of the base class will be run during the call, instead of calling the method with the new keyword in the subclass. Override is only used when it is inherited. It is used in the method where the subclass needs to override the base class method. When a subclass method uses the override keyword, the corresponding method in the subclass is run during the call.

If you use override, the system will find the test () of its substantive class, whether it is calling test () in Class A or Class B ();
If new is used, you can call test () of the base class through type conversion ();

In short, "new" looks at the left side of the equals sign, and "Override" looks at the right side of the equals sign.

The following is the case of override:
A A = new ();
B = new B ();
A a1 = new B ();
A. Test (); // call test () in ();
B. Test (); // call test () in B ();
A1.test (); // call test () in B. The system automatically recognizes instances where A1 is B.
(A) B). Test (); // same as above

The following is the case of new:
A A = new ();
B = new B ();
A a1 = new B ();
A. Test (); // call test () in ();
B. Test (); // call test () in B ();
A1.test (); // call test () in ();
(A) B). Test (); // same as above
Another example

Public class parentclass // base class
{
Public parentclass ()
{
Console. writeline ("constructor in the base class ");
}
Public Virtual void method () // use virtual to use override in the subclass, and new does not need this
{
Console. writeline ("method of the base class ()");
}
}

Public class childclass1: parentclass // inherit the base class and check the override status.
{
Public childclass1 ()
{
Console. writeline ("childclass1 constructor ");
}
Public override void method () // override is used to redefine the methods of the base class.
{
Console. writeline ("childclass1 method () Use override ");
}
}

Public class childclass2: parentclass // inherit the base class and check the new status.
{
Public childclass2 ()
{
Console. writeline ("childclass2 constructor ");
}
New public void method () // use new, not to say that the base class method is used, but to redefine a subclass method, except that the method name is the same as the base class
{
Console. writeline ("childclass2 method ()");
}
}
Public class test
{
Static void main ()
{
Parentclass parent = (parentclass) New childclass1 (); // use the override subclass to box a base class object handle
Parent. Method ();
Parentclass newparent = (parentclass) New childclass2 (); // use the new subclass to box a base class object handle
Newparent. Method ();
Childclass2 newparent1 = new childclass2 (); // a subclass handle
Newparent1.method ();
}
}

Running result:

Construction Method of base class
Childclass1 Constructor
Childclass1 method () Use override
Construction Method of base class
Childclass2 constructor // shows the inherited features
Method () of the base class ()
Construct methods in the base class // demonstrate the inherited features
Childclass2 Constructor
Childclass2 method ()

 

Inheritance

 

Member fields and static functions cannot be declared as virtual, because this concept is only meaningful to instance function members of the class. Virtual declarations are not required for abstract functions.

If type nesting exists, the internal type can always access all members of the external type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.