C # class and object method body and local variable Specification

Source: Internet
Author: User

Asp tutorial. net c # Language Specification class and object method body and local variables

The method Body specifies the statements that will be executed when the method is called.

The method body can declare variables used only in this method call. Such variables are called local variable ).

The local variable declaration specifies the type name, variable name, and initial value.

The following example declares a local variable I with an initial value of zero and a variable j without an initial value.


Using system;
Class squares
Static void main ()
Int I = 0;
Int j;
While (I <10)
J = I * I;
Console. writeline ("{0} x {0} = {1}", I, j );
I = I + 1;

C # the value of a local variable must be obtained only after a definite value (definitely assigned) is assigned.

For example, if the initial value is not included in the declaration of I, the compiler will report an error for subsequent use of I, because I has not explicitly assigned values to these positions in the program.

You can use the return statement to control the caller returned to it.

In the return void method, the return statement cannot specify an expression.

In a non-void return method, the return statement must contain an expression for calculating the return value.

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: 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.