A description of methods in C #

Source: Internet
Author: User
Preface:

The methods in C # are often used in C # programming, so what is a method and how does it work? Summing up here, I hope you can point out my shortcomings.

1 What is a method

method is a mechanism for reusing a bunch of code in C #. The method is a piece of code that may have input values and may return a value. Remember that Mr. Rice has spoken before about a method (function) Metaphor: A method is like a pot, and our rice and water are like parameters, put rice and water into the pot, then you can cook. The method is that we need to give it some parameters, and then it might give us some return values.
There are a number of methods in the. NET Framework, such as the Show method in the MessageBox class, the WriteLine method in the Console class, the ReadLine method, the Int32 parse method, and the next method of the random class. Skilled use. The methods defined in the class library in NETFramework will make writing programs faster and faster.

2 How methods are defined
Method is declared in a class or struct, when declared, you need to specify the access level, return value, method name, and any method arguments. The method arguments are enclosed in parentheses and separated by commas. An empty parenthesis indicates that the method does not require parameters.
Such as:
Example 1:

public static void SayHello () {     System.Console.WriteLine ("hello,world!");}

Example 2:

  public static int Add (int x,int y)        {            int z=x+y;            return z;        }

The definition of 2 common methods is defined above. Example 1 has no parameters, and Example 2 has 2 parameters. If you want to pass parameters to a method, you only need to provide those parameters in parentheses when you call the method. For the called method, the passed-in variable is called "parameter".
The parameters that the method receives are also provided in a set of parentheses, but you must specify the type and name of each parameter. The name does not have to be the same as the parameter.

3 Invocation of the method

the method only needs to pay attention to the parameter when it is called, and the method's return value problem. Call the method SayHello () as defined in Example 1. Enter the code statement SayHello () directly inside another method. Call the method Add () as defined in Example 2. Enter the code statement directly inside another method

INTA = 1, b = 2,c;  c = Add (A, b);

The return value of the 4 method

Method can return any type of value, nor can it return any value. The return value type is reflected in the method's head.

Example: Method definition Format: public int Method1 (int x,int y)

You can see that the return value type of the Method1 method is int.

The return value of a method is often brought past by a return statement. The return statement can terminate the execution of the method it appears in and return control to the code that invokes the method. If the method is of type void, you can omit the return statement. You can observe the following code:

Publicstatic string Getstr ()        {                      string str=string. Empty;            return str;            Str =system.console.read (). ToString ();      }


5. Overloading of methods

What is method overloading? In high-level languages such as objects, we are allowed to define multiple methods with the same name, number of parameters, and different order of parameters in a class, which we call parameter lists differently for different number of arguments or for different parameter lists. It is important to note that the return value of the method is not mentioned here. That is, determining whether a method constitutes an overload has the following conditions:
in the same class;
method name is the same;
parameter list is different.
in the overloads of a method, an explicit first match method that calls a method is the same as a given number of arguments
ref and out cannot be used as an overload, only one option is required, and two cannot be used at the same time
if the parameter is decorated with ref or out, the argument must be a variable
The return value of the method does not constitute an overloaded condition. The number of arguments to the
method can be overloaded, and when the method has the same number of arguments, it can be overloaded with a different type of parameter. The
uses the array type as the parameter of the method: The keyword params can appear only once and is the last one that appears in the parameter.

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.