Declaration of the C # method

Source: Internet
Author: User
Tags abstract modifier modifiers

A method is a member of a class that is used to perform calculations or other behaviors. Let's look at the declaration format of the method:

Method-header Method-body

The format of the method header Method-header:

Attributes Method-modifiers Return-type member-name (formal-parameter-list)

The parameters passed to the method are declared in the formal parameter table Formal-parameter-list of the method, which we will discuss in detail later.

In the declaration of a method, at least the method name, modifier, and parameter type should be included, and the return value and parameter names are not required.

Note: The method name Member-name should not have the same name as the other methods in the same class or the other member names in the class.

Modifiers

The modifier method-modifier of the method can be:

New

Public

Protected

Internal

Private

Static

Virtual

Sealed

Override

Abstract

extern

For methods that use the abstract and extern modifiers, the execution body of the method method-body only a simple semicolon. All other method bodies should contain the statements to be executed to invoke the method.

return value

The type of the return value of a method can be a valid data type for C #. C # Gets the return value in the execution part of the method, such as:

Program Listing 11-1:

Using System;
Class Test
{public
  int max (int x,int y) {
    if (x>y) return
       x;
    else return
       y;
  }
  public void Main () {
     Console.WriteLine ("The max of 6 ADN 8 is:{0}", Max (6,8));
  }

The output of the program is:

The max of 6 and 8 Is:8

If you do not follow any value after return, the method returns the value is void.

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.