C # Declaration Method Instance Description _ Basic Application

Source: Internet
Author: User
Tags visual studio


1. Specify method declaration syntax

The syntax format for the Microsoft Visual C # method is:

ReturnType MethodName (Parameterlist)
{
Add method Principal statement here
}

The ReturnType (return type) is a type name that specifies what type of information the method returns. This can be any type, such as int or string. If you want to write a method that does not return a value, you must replace the return type with the keyword void.

The MethodName (method name) is the name that is used when the method is invoked. The method name follows the same identifier naming rule as the variable name. For example, Addvalues is a valid method name, and add$values is not valid. Currently, you should use the CamelCase naming style for method names, and you should start with a verb to make the method more visible, such as Displaycustomer.

The parameterlist (argument list) is optional, describing the type and name of the information that can be passed to the method. When you fill in the variable information in parentheses, you write the type name of the parameter and then the parameter name, as you would when declaring the variable. If the method has two or more parameters, you must separate them by using commas.

The method principal statement is the line of code to be executed when the method is invoked. They must be placed between the start and end braces ({}).

Important c,c++ and Microsoft Visual Basic Programmer Note that C # does not support global methods. All methods must be inside a class, otherwise the code will not compile.

The following is the definition of a method named Addvalues that returns a value of type int and can receive two parameters of type int, named Lefthandside and Righthandside respectively.

int addvalues (int lefthandside, int righthandside)
{
// ...
Add method Principal statement here
// ...
}

The following is a definition of a method named Showresult that does not return any value and can receive an int parameter named answer:

void Showresult (int answer)
{
// ...
}

Note that if the method does not return any values, then the VOID keyword must be used.

Important Visual Basic Programmers Note that C # does not allow different keywords to distinguish between a method that returns a value (that is, a function) and a method that does not return a value (that is, a procedure or subroutine). In C #, either a return type is specified or void is specified.

2. Return statement

If you want a method to return information (in other words, its return type is not void), you must write a returns statement inside the method. To do this, first write down the keyword return, write an expression (it calculates the value to be returned), and then write a semicolon. The type of an expression must be the same as the return type specified by the function. In other words, a return statement must return an int value if a function returns an int value. Otherwise, the program will not compile. Examples are as follows:

int addvalues (int lefthandside, int righthandside)
{
// ...
return lefthandside + righthandside;
}

The return statement should be at the end of the method because it will result in a method ending. Any statements after the return statement are not executed (the compiler will therefore be warned if you persist in adding additional statements after the return statement). If the method is not prepared to return information (the return type is void), you can use a variant of the returns statement to immediately exit from the method. In this case, you need to write down the keyword return, followed by a semicolon. For example:

void Showresult (int answer)
{
Show answers
...
Return
}

If the method returns no information, the return statement can also be omitted because once the closing brace (}) is executed to the end of the method, the method ends automatically. However, while this is a common form of writing, it is not a good programming habit.

Definition of research methods

1. Start Visual Studio 2005.

2. Open the 3\methods item in the \microsoft Press\visual CSharp Step by step\chapter methods subfolder in My Documents folder.

3. Select "Debug" | " Start execution (without debugging). Visual Studio 2005 starts to build and run the application.

4. Experience the operation of the application, click Quit.

5. Display Form1.cs code in the Code and Text Editor window (right-click Form1.cs in Solution Explorer and select View Code from the pop-up menu).

6. In the Code and Text Editor window, locate the Addvalues method, as follows:

private int addvalues (int lefthandside, int righthandside)
{
Expression. Text = lefthandside.tostring () + "+" + righthandside.tostring ();
return lefthandside + righthandside;
}

The Addvalues method contains two statements. The first statement displays the calculation to perform in the Expression text box on the form. The lefthandside and Righthandside parameter values are converted to strings (using the ToString method described in chapter 2nd) and are connected using a "+" symbol between the two.

The second statement uses the operator + to find the sum of the two int variables lefthandside and righthandside, and returns the result. Remember, two int values are added to the int value, so the return type of the Addvalues method is set to int.

7. Find the Showresult method in the Code and Text Editor window, as follows:

private void Showresult (int answer)
{
Result. Text = answer. ToString ();
}

This method has only one principal statement, which is to display the answer string form in the result text box.

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.