Methods in C #

Source: Internet
Author: User

Remember our first program? Forget it? Then you have to work hard. Our first program is to output some strings to a DOS window. In which program there is only one method, the main method. The main method is a special method, but it is also a method. Why is it that the main method is special? You can check it online. If you can't find it, remember to ask me oh.

C # programs are made up of a number of classes. Each class contains a number of methods. We can not take care of what the class is. In the practice before us, we all met the Program.cs. Program is a class. Let's take the class as a container for the moment. We can put the method in the container of the class. The practice behind us will put the methods in the program class.

Define a method

Static return type method name (parameter type parameter 1 name, parameter type parameter 2 name)

{

Your code

return value

}

Static: What do you mean by not having a tube? Before you say what it means, add it in every method you write.

Return type: The data type we talked about earlier, int,string, and so on. Usually a method call is given to return a value where this method is called. What if my method doesn't want to return? There is no way. Ah? I'm joking with you. If your method does not have anything to return, the return type is void.

Method Name: You can give the method a name you want. Can't afford to be blind. Naming rules are the same as naming rules for variables. Have you forgotten? Let's take a look at the front stuff.

Parameters: Without parameters, the parentheses inside will not write anything, need a few parameters, write a few parameters, the parameters are separated by commas. What is the parameter type? It seems that your perception is not enough. is the data type. What is the name of the parameter? Oh my God! Is the same as the variable name.

Why should we introduce a method?

Real-World program functions are more complex. A program consists of dozens of or even tens of thousands of functions. If we put all these functions in one place, such as our main method, then this method will be too complicated. I do not know Lego toys, we have not played. Through a small plug-in, can be assembled into buildings, ships and even monsters. The idea of modern program design is similar to that of Lego toys. A complex program is decomposed into many relatively simple units. The combination of these simple units can make up many different kinds of programs. Many of the common units can be reused, greatly improving the speed of development. And these are the elements that we can understand here as methods. And in the actual development of a program, usually by a team of several people to work together to complete. After splitting a program into different ways, everyone can focus on the part they are responsible for. When we are all done, we will assemble together.

How do I invoke a method?

Add the following statement where you need to call.

Method name (variable 1 or a data value, variable 2 or a data value); Where the variable 1 or a data value corresponds to the first parameter of the method, the variable 2 or a data value corresponds to the second parameter of the method. The type of a variable or data value is the same as the type of the parameter in the method definition.

If the method has a return value, you want to save the return value. Then just define a variable, the type of the variable needs to be the same as the type of the method's return value. This variable is to be followed by the return value of the function. form as follows.

numeric type variable name;

Variable name = Method Name (variable 1 or a data value, variable 2 or a data value);

Let's take a look at the actual code and understand the C # method more specifically.

In this exercise, we created three new methods in the class program. The first method has a parameter. The second method has two parameters. The third method takes no arguments. The function of three methods is very simple, is to output some characters, if there are parameters, the value of the parameter output. We then call these three methods in turn in the main method.

You create a project. The solution name is Sloution12, the project name is Exercise12, and the project type is a console program. Click Program.cs. Add the code to the inside.

Run results

Practice

1. Create a new method. It is then called in the Main method.

2. Try to get some strange names for this method. Look at the names that are not allowed by C #.

3. Call a method with parameters, can not pass parameters to him?

4. Call a method without parameters, can pass parameters to him?

5. Create a new method to output something. Then modify the method Printwithoneparameter, calling the new method you created. Run the program and see the results.

Methods in C #

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.