Visual C # 2005 Quick Start Declaration method

Source: Internet
Author: User
Tags valid

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

1. Specify method declaration syntax

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

returnType methodName ( parameterList )
{
  // 这里添加方法主体语句
}

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)
{
  // ...
  //这里添加方法主体语句
  // ...
}

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.

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.