① do operators
For creating objects and calling constructors, small chestnuts A is as follows:
Class1 obj new Class1 ();
To create an instance of an anonymous type, small chestnut B is as follows:
var from inch Customers Select New {Name = cust. Name, Address = Cust. Primaryaddress};
the default constructor for calling a value type , small chestnut C is as follows: where i is initialized to 0, it is the default value of type int . the effect of the statement is equal to: int i=0;
int New int ();
Declaring the default constructor for a struct is an error, because each value type implicitly has a public default constructor. you can declare a parameterized constructor on a struct type to set its initial value, but you must do so only if you want a value other than the default value.
Value type objects, such as structs, are created on the stack, whereas reference type objects, such as classes, are created on the heap.Both types of objects are automatically destroyed, but objects based on value types are destroyed when they are out of scope, whereas objects based on reference types are destroyed at an indeterminate time after the last reference to the object is removed.for reference types that occupy fixed resources, such as large amounts of memory, file handles, or network connections, it is sometimes necessary to use deterministic termination to ensure that objects are destroyed as soon as possible. The new operator cannot be overloaded.
② do modifiers to hide inherited members from base class members
as a modifier, the basic rule can be summed up as: Implementing a hidden method in a derived class, the base class method must be defined as virtual;new as a modifier, and override coexistence when implementing a hidden base class member, because the semantics repel: New is used to create a fresh member, Also hides a member of the same name in the base class, and override is used to implement an extension of the base class member.
Small chestnuts B are as follows:
classNumber { Public Static inti =123; Public voidShowinfo () {Console.WriteLine ("base class---"); } Public Virtual voidShownumber () {Console.WriteLine (i.ToString ()); } } classIntnumber:number {New Public Static inti =456; Public New Virtual voidShowinfo () {Console.WriteLine ("Derived Class---"); } Public Override voidShownumber () {Console.WriteLine ("Base number is {0}", number.i.tostring ()); Console.WriteLine ("New number is {0}", i.ToString ()); } } Static voidMain (string[] args) {Number num=NewNumber (); Num. Shownumber (); //123Intnumber Intnum =Newintnumber (); Intnum.shownumber (); //123 456 Number Number=Newintnumber (); //who actually called it? Number. Showinfo ();//base class---//who actually called it? Number. Shownumber ();//The derived class overrides Shownumber (), so the output is: 123 456Console.readkey (); }
Result of Operation:
③ as a constraint
The new constraint specifies that any type parameter in a generic class declaration must have a public parameterless constructor. If you want to use the new constraint, the type cannot be an abstract type. When used with other constraints, thenew () constraint must be specified last.
The role of the keyword new