C # Language Series Lectures (6) Method

Source: Internet
Author: User
● The method, also known as the member function, embodies the behavior of classes or objects.

● Methods include static methods and instance methods. The static method can only operate on static domains, while the instance method can operate both static domains and instance domains.

● The methods are five access Modifiers like the domain: public, protected, internal, protected internal, and private.

Method Parameters

The method is also called a member function ).

There are four types of parameter transfer: by value, by reference, output, and array ). There is no additional modifier for the value passing parameter. The address passing parameter requires the modifier ref, the output parameter requires the modifier out, And the array parameter requires the modifier Params. If the value of the parameter is changed during a method call, the parameters of the passed-in method do not change after the method call is completed, but retain the original value. On the contrary, if the value of the parameter is changed during the method call process, the parameters for passing in the method also change after the call is completed. In fact, we can clearly see the meaning of the two from the name-the pass-through parameter transfers a copy of the call parameter, while the transfer parameter passes the memory address of the call parameter, this parameter points to the same storage location inside and outside the method. Note that the ref modifier must be added to the method call of the address passing parameter at the time of declaration or call.

When values and addresses are passed in, C # requires the user to explicitly initialize the parameters before they are passed in; otherwise, the compiler reports an error! But what if there is a function that does not depend on the initial value of the parameter, but only needs to get its value when the function returns it (this technique is especially required when the function returns more than one value )? The answer is to use output parameters modified by out. However, you must note that the output parameters are different from the common function return values: the function return values are often stored in the stack and popped up upon return. The output parameters need to be pre-specified for storage, that is, the user needs to declare the variable in advance-of course, the variable can also be initialized.

All output parameters in the function body must be assigned values; otherwise, the compiler reports an error! The out modifier should also be used in function declaration and call. In addition to the special function of acting as the return value, the out modifier is similar to the ref modifier: The address passing. It can be seen that C # completely abandons the great degree of freedom that traditional C/C ++ languages give programmers. After all, C # Is a next-generation efficient network platform for development, security-including System Security (system structure design) and engineering security (avoiding errors that programmers often make) are important considerations during design, of course, C # does not lose the performance of many languages due to security. This is exactly what C # is doing!

Array parameters are used to pass a large number of array set parameters. Array parameters can be arrays (such as VAR) or implicitly converted to arrays, for example, 10, 20, 30, 40, and 50. This provides high scalability for the program.

Different method parameters with the same name may lead to method polymorphism, which is also called the overloading method. It should be noted that the compiler binds methods and method calls at compilation. Methods can only be overloaded by different parameters. Other differences (such as return values) cannot provide effective overloaded information for the compiler.

Method inheritance

The C # method introduces virtual, override, sealed, and abstract modifiers to provide different inheritance requirements. The virtual method of a class can change its implementation method in the inherited class of the class. Of course, this change is limited to the change of the method body, rather than the change of the method header (method declaration. The virtual method changed by the quilt class must be indicated by override in the method header. When a virtual method is called, the instance of this class, that is, the run-time type of the object, determines which method is called. See the following example:

Using system;

Class Parent

{Public void F (){

Console. writeline ("parent. F ");}

Public Virtual void g (){

Console. writeline ("parent. g ");}

}

Class child: parent

{New Public void F (){

Console. writeline ("child. F ");}

Public override void g (){

Console. writeline ("child. g ");}

}

Class Test

{

Static void main ()

{Child B = new child ();

Parent A = B;

A. F ();

B. F ();

A. G ();

B. G ();

}

}

After the program is compiled and executed, the output is:

Parent. f

Child. f

Child. g

Child. g

We can see that the F () method declaration in the Child class adopts the rewrite (new) method to shield the non-virtual method F () declaration in the parent class, while G () methods Use override to provide method polymorphism. Note that the rewrite (new) method is different from the override method. In essence, the rewrite method is bound at compile time, and the override method is bound at runtime. It is worth noting that virtual methods cannot be static methods -- that is to say, static and virtual methods cannot be modified simultaneously, which is determined by its runtime type discrimination mechanism. Override must be used together with virtual, so it cannot be used together with static.

What should I do if I do not want to overwrite a virtual method in a class inheritance system? The answer is sealed override (sealed override). You can use sealed and override to simultaneously modify a virtual method: sealed override public void F (). Note that sealed and override must be used at the same time. Sealed must also cover a virtual method or a virtual method that is overwritten (instead of sealed. Sealing a non-virtual method is meaningless and incorrect.

Abstract methods are logically similar to virtual methods, but cannot be called as virtual methods. They are declarations rather than implementations of an interface. Abstract methods cannot be implemented or implemented. Abstract methods cannot be static. Classes containing abstract methods must be abstract classes and abstract class modifiers must be added. Abstract classes do not have to contain abstract methods. The subclass of an abstract class that inherits an abstract method must be overwritten and implemented (override is used directly). This method can be combined with abstract override to continue abstraction, or no overwrite or implementation is provided, the behavior of the latter two is the same. See the following example:

Using system;

Abstract class parent

{Public abstract void F ();

Public abstract void g ();}

Abstract class child: parent

{Public abstract override void F ();}

Abstract class grandson: Child

{

Public override void F ()

{Console. writeline ("grandson. F ");}

Public override void g ()

{Console. writeline ("grandson. g ");}

}

Abstract METHODS can abstract an inherited virtual method. By grasping the basic mechanism of binding during runtime and compilation, you can see through the overload, virtual, override, sealed, abstract and other methods.

External Method

C # introduces the extern modifier to represent external methods. External methods are implemented in languages other than C #, such as Win32 API functions. External methods cannot be abstract methods. Let's take a look at the following example:

Using system;

Using system. runtime. interopservices;

Class myclass

{[Dllimport ("user32.dll")]

Static extern int messageboxa (INT hwnd, string MSG, string caption, int type );

Public static void main ()

{Messageboxa (0, "Hello, world! "," This is called from a C # app! ", 0 );}

}

After the program is compiled and executed, the output is:

The Win32 API function int messageboxa (INT hwnd, string MSG, string caption, int type) is called here ).

Related Article

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.