Chapter 3 induction of knowledge points and Chapter 3 knowledge points
1. Object Initiator
Initialize the object directly when calling the constructor.
public class Student{ public string Name; public int Age; public bool Gender;}Student stu=new Student{Name="zhangsan",Age=10,Gender=false};
Equivalent:
Student stu=new Student();stu.Name="zhangsan";stu.Age=10;stu.Gender=false;
2. this and base
This is the reference class instance itself.
public class Student{ string name; public Test(string name){this.name=name;}}
Base function: 1. Access the overloaded base class method member from the subclass 2. Call the construction method of the base class
Reference the example in the book:
public class Asset{ public string name; public virtual decimal Liability{ get { return 0; } }}public class Home : Asset{ public decimal Mortgage; public override decimal Liability { get { return base.Liability + Mortgage; } }}
3. packing and unpacking
Packing is to convert the value type to the reference type.
Int num = 10;
Object obj = x; // boxed int type
The case is to convert the reference type to the value type.
Object obj = 10;
Int num = (int) obj;
The essence of packing and unpacking is copying: Packing refers to copying a value-type instance to a new object, and disassembling refers to copying the content of the object back to a value-type instance.
4. covariant and Inverter
Covariant: Assume that A is A subclass of B. If C <A> can be referenced and converted to C <B>, C is called A covariant class.
IAbc <string> a = ...;
IAbc <object> B =;
IAbc <T> is a covariant class.
Inverter: Assume that A is A subclass of B. If C <B> can be referenced and converted to C <A>, C is called an inverter class.