. NET Method Overview

Source: Internet
Author: User

 

  • Method
A method is a code block containing a series of statements. In C #, each execution command is completed in the context of the method. When declaring a method in a class or structure, you must specify the access level, return value, method name, and any method parameters. Method parameters are enclosed in brackets and separated by commas. Empty parentheses indicate that parameters are not required for the method. Methods can have or do not have parameters, or have or do not return values.
  • Main Method
The Main method is the entry point of the console application or window application. The Main method is the entry point of the. exe program, where the program control flow starts and ends. Main is declared in a class or structure. Main must be static and should not be public. However, it is not required that the closed class or structure be static. There are two types of return for Main: void or int. The declared Main method can have string [] parameters that contain the real arguments of the command line, or do not have such parameters. When using Visual Studio to create a Windows form application, you can manually add the form parameter or use the Environment class to obtain the real arguments of the command line. The form parameter is read as the command line real parameter for indexing from scratch. Unlike C and C ++, C # does not regard the program name as the first real argument of the command line.
  • Heavy Load
Defines members with the same name, but their parameter quantity or type is different.
  • Parameters
For called methods, the input variables are called "Parameters ". Parameters received by the method are also provided in a set of brackets, but the type and name of each parameter must be specified. This name does not have to be the same as the parameter.
  • Parameter Modifier
  • None: value transfer, a copy of the original data
  • Out: References are passed. If the method is not assigned a value to this parameter, a compilation error occurs.
  • Ref: References are passed. No compilation error will occur if this parameter is not assigned by the method.
  • Params: Allows passing a set of variable numbers of parameters as separate logical parameters. A method can only have one params and must be the last parameter in the method.
  • Parameter transfer
  • Value Transfer (default)
  • Value Type: transmits a copy of the data value.
  • Reference Transfer
  • Out
  • Ref
  • Optional Parameter
  • Specify the default value of the parameter. The default value must be determined at compilation, but not at runtime. It can only be placed at the end of the method parameter.
    • Name parameter call Method
    • Return Value
    Methods can return values to the caller. If the return type (the type listed before the method name) is not void, the return keyword can be used to return the value.
    • Return Value Modifier
  • No return value: void
  • Returned value: type name of the returned value
    • Method Signature
    • Access Modifier
  • Private: private. The class can only be accessed.
  • Protected: protected, accessible to the class itself and its derived classes
  • Internal: internal, the same Assembly internal type can access
  • Public: public, both internal and external access
  • Optional Modifier
      • Virtual: virtual
      • Static: static
      • Abstract: abstract
      • Sealed: Closed
      • Override: inherited

    Related Article

    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.