Nineth Chapter method [". NET Framework Program design" Reading notes]

Source: Internet
Author: User
Tags define definition constructor functions net reference variable access
. NET Framework | notes | procedure | Design Nineth Chapter Method




One, instance Builder

1. The trilogy mentioned earlier about creating objects with the new operator:

L Allocate memory for an object

L Initializes additional members of the object (method table pointers and Syncblockindex)

L Invoke Instance Builder initialization instance state

When allocating memory, the system places all memory locations to 0 values, which is why the field is initialized without assigning a value of 0 or null.



Do not invoke the instance constructor:

L INVOKE the Object.memberwiseclone () method to create the instance (allocate memory; initialize additional members; Copy bytes of source objects to newly created objects)

L when deserializing an object

2. To avoid generating too many constructor codes for instance fields, you should avoid De Fu values when declaring fields, but instead Fu Yan values for them in the parameterless constructor, calling the parameterless constructor in other overloaded constructors.



3, Value type instance Builder

L The C # compiler does not automatically call its constructors, and you must explicitly call the constructor to function

L The C # compiler does not allow the definition of a parameterless instance constructor for a value type (the following describes a parameter-free type constructor)

L cannot assign an initial value to a field in a struct at the same time as a declaration, which can be done by defining a join constructor

L must assign an initial value to all fields in the constructor of the struct

Second, type Builder

1. Some limitations of type Builder:

L can't take any parameters.

The L type constructor is always private and cannot be used with other access modifiers

2. The time the type constructor is invoked:

L The first instance is created, or the first field or member of the type is first accessed

L Non-inheriting static field before first accessed

A type constructor is invoked only once in the life cycle of a type;

3. Some restrictions:

L If an exception is thrown in a type constructor, the type becomes inaccessible, and any field or method accessing it throws a System.TypeInitializationException exception

The L Type Builder can access only static fields of type

The L type constructor should not call the type constructor of the base type, because static fields are not inherited but are compile-time static bindings

Third, operator overloading

1, operator overload

Some restrictions on operator overloading in C #:

L must be declared as public static

L must have a parameter for the type of the operator

L cannot change the argument number of the original definition of the operator

L If you define the true operator, you must also define the false operator, both of which must return a bool value

L + + 、--operator must return an instance of the type to which it belongs

L can be overloaded with unary operators: + 、-、! , ~, + + 、--、 true, False

L can be overloaded with the two-dollar operator: + 、-、 *,/,%,! , ^ (XOR), <, >, <<, >>, = =,! =, <=, >=

L is not allowed to be overloaded operator:&&, | |, =,? :, + =, =,/=,%=, |=, ^=, <<=, >>=, and in fact some of these "duplex operators" are automatically generated when the two-dollar operator is overloaded, not explicitly defined

L must be a pair of overloaded operators: (= =,! =), (<,>), (<=,>=)

L + + 、--operator overload can not distinguish between its predecessor or post

2, operator overload and language interoperability

The compiler wildcard a special name for overloaded operations, such as The + (plus) operator to generate the Op_addition () method and add SpecialName tags to the method's definition entry. When an operator overload is not available for a language, you can directly define a method with that particular name to call in another language, or call a method with that particular name directly to accommodate a language that cannot resolve operator restrictions. Such as: VB can not overload operators, you can explicitly define the Op_addition () method to call in C #, C # Definition of the + operator can not be recognized by VB, you can explicitly call the Op_addition () method to obtain the same function.

Iv. Conversion Operators

Some restrictions on conversion operators:

L must be public static

L must specify the keyword implicit or explicit, the principle is: convert from this type to other types using implicit, convert other types to this type with explicit, not all use implicit



Five, the method parameter

1, reference parameters

L PASS the value by default

L flags are out parameters that do not have to be initialized before the method is invoked, but must be assigned before returning, parameters that are not initialized cannot be used

The l flag is a parameter of ref that must be initialized before the method is invoked, otherwise triggering a compilation error

L can use ref or out to overload a method, but not to overload a method by distinguishing between ref and out

L a variable (argument) passed by reference must be exactly the same type as the parameter (formal parameter) declared by the method, otherwise triggering a compilation error.

2, variable number of parameters

Specifies a sequence of variable parameters using the params keyword and an array of objects. Some restrictions:

L only the last parameter of a method can use a variable number of parameters



VI. Virtual method

1, the call mechanism of virtual method

The CLR invokes the method using the following two IL directives:

U call invokes a method based on the type (that is, the static type of the reference, the declaring type)

U callvirt invokes a method based on the object (that is, the dynamic type of the reference, the actual type)

For virtual methods that are invoked using call:

L Base. Virtual method (),

L sealed type refers to virtual method because there is no need to verify the actual type of the sealed type

L value type, avoid boxed

To invoke a Non-virtual method using callvirt:

l When applying a variable to NULL, a System.NullReferenceException exception is thrown using callvirt, and call does not throw

Whether call or callvirt invokes a method, there is an implied this pointer as the first parameter of the method, which points to the object being manipulated

2, the virtual method version control:

Use the following example to illustrate:

Using System;



Class BaseClass

{

public void Nonvirtualfunc ()

{

Console.WriteLine ("Non virtual Func in base class");

}



public virtual void Virtualfunc ()

{

Console.WriteLine ("Virtual Func in base class");

}

}



Class Devicedclass:baseclass

{

The compiler will have warning if you do not use the New keyword:

Required keyword on "devicedclass.nonvirtualfunc ()"

New, because it hides the inherited member "Baseclass.nonvirtualfunc ()"

Public new void Nonvirtualfunc ()

{

Console.WriteLine ("Non Virtual Func in deviced class");

}

If you do not add the keyword override or new, the compiler will have warning:

"Devicedclass.virtualfunc ()" Hides inherited members "Baseclass.virtualfunc" ()

”。 To enable the current member to override the implementation, add the keyword override. Otherwise, add keywords

New

public override void Virtualfunc ()

{

Console.WriteLine ("Virtual func in deviced class");

}

}



Class TestClass

{

public static void Main ()

{

Non-virtual and virtual functions are called by derived class instances

Devicedclass dc = new Devicedclass ();

dc. Nonvirtualfunc ();

dc. Virtualfunc ();



Base class instance calls Non-virtual and virtual functions

BaseClass BC = new BaseClass ();

Bc. Nonvirtualfunc ();

Bc. Virtualfunc ();



Base class reference call to a derived class instance non-virtual and virtual functions

BaseClass BC1 = DC;

BC1. Nonvirtualfunc ();

BC1. Virtualfunc ();

}

}



/*

Using the override of the keyword on a virtual function:

Non virtual Func in deviced class

Virtual Func in deviced class

Non virtual Func in base class

Virtual Func in base class

Non virtual Func in base class

Virtual Func in deviced class

*/



/*

Running results with keyword new on virtual functions

Non virtual Func in deviced class

Virtual Func in deviced class

Non virtual Func in base class

Virtual Func in base class

Non virtual Func in base class

Virtual Func in base class

*/

Visible from: New and override the control of versions in a derived class, and in chapter seventh you see that oeverride can only be used for virtual methods, and new can be used for non-virtual or virtual methods to implement a method that hides the same name in a base class. Using override on virtual functions, the method of overriding the base class is not hidden, and this realizes polymorphism. We can assume the conclusion that new calls the static type method using the call instruction, and override uses the callvirt instruction to invoke the method of the dynamic type.

I hope this example is helpful to your understanding.


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.