C # Declarative Methods

Source: Internet
Author: User

Method is a named set of statements. If you have used other programming languages such as C or Visual Basic before, you can treat a method as something similar to a function or subroutine. Each method has a name and a principal. The method name should be a meaningful identifier, and it should describe the purpose of the method (such as Calculateincometax). The method body contains the statements that were actually executed when the method was called. You can provide some data for most methods to process and let it return some information (usually processing the results). Method is a basic, powerful programming mechanism.

1. Specifying method declaration syntax

The syntax format for Microsoft Visual C # methods is:

ReturnType MethodName (parameterlist)
{
Add method Body Statement here
}

A 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 called. 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 the method name, and you should start with a verb to make the use of the method more straightforward, such as Displaycustomer.

The parameterlist (parameter list) is optional and describes the type and name of information that can be passed to a method. To fill in the variable information in parentheses, write the type name of the argument, and then the parameter name, as you would declare the variable. If the method has two or more arguments, you must separate them with commas.

The method principal statement is the line of code to execute when the method is called. They must be placed between the starting and closing curly braces ({}).

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

The following is a definition of a method named Addvalues, which 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 Body Statement here
// ...
}

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

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

Note that if the method does not return any values, you must use the Void keyword.

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

2. Return statement

If you want a method to return information (in other words, its return type is not void), you must write a return statement inside the method. To do this, first write the keyword return, write an expression (which calculates the value to return), 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, if a function returns an int value, then the return statement must return 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 the completion of the method. Any statements after the return statement are not executed (the compiler will warn you if you persist 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 return statement to immediately exit from the method. In this case, you need to write the keyword return first, followed by a semicolon. For example:

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

If the method does not return any information, you can also omit the return statement because the method ends automatically when you execute the closing curly brace (}) at the end of the method. However, while this is a common notation, it is not a good programming habit.

Research Methodology definition

1. Start Visual Studio 2005.

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

3. Select "Debug" | " Start execution (do not debug). Visual Studio 2005 will start building and running the application.

4. Try running the application and click Quit.

5. Display the Form1.cs code in the Code and Text Editor window (right-click Form1.cs in Solution Explorer and choose 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 consists of 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 + Lefthandside and righthandside to find the sum of the two int variables and returns the result. Remember that the two int value is also an int value, so the return type of the Addvalues method is set to int.

7. Locate 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 the function of displaying the answer string form in the result text box.

C # Declarative Methods

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.