C # Formal parameters, arguments, value pass arguments, reference pass parameters, output parameters, parameter array learning (i)

Source: Internet
Author: User

1) Formal parameters

A parameter, as its name suggests, is a formal argument, not an actual argument, which replaces the value of the actual incoming method. The value itself participates in the operation in the method body code. A formal parameter is defined in a parameter, unlike a local variable in a method body, because it is a variable that does not allow a local variable of the same name to exist within its scope, regardless of whether their type is the same or not.

Look at the following code example:

The strName of this is that a formal parameter is also a variable and does not allow the occurrence of a local variable public
        void Sayhelloto (string strName)
        {
            Console.WriteLine (" My name is {0} ", strName);
        }

Characteristics of formal parameters:

A ① parameter is a variable that has all the characteristics of a variable. The ② method can have multiple formal parameters, which are separated directly by commas, even if multiple parameters of the same type cannot be merged.

2) argument

The argument is relative to the formal parameter, the formal parameter is the "double" of the actual value, and the actual value represented by the alias is the argument. The actual value can be a specific value, or it can be a variable

Look at the following code example:

static void Main (string[] args)
        {program
            Pro = new program ();
            int nnum = ten;
            When the Add method is invoked, two arguments are passed, the first is the actual value,
            //The second nnum is a variable, but the
            int nresult = Pro is initialized when the method is passed in. ADD (Nnum);
        }

        The a,b here is called the formal parameter public
        int ADD (int a, int b)
        {return
            a + b;
        }

The actual value of the ① argument used for the initial Huafing parameter or the expression ② argument is in the list of method arguments to invoke.

3) passing parameters by value

The value parameter is copied to the formal parameter by the value of the argument. To implement the value passed to the method, which is called by value, when the method is called, the CLR does the following:

① ② The value of the argument to the formal parameter in the managed stack for the parameter allocation space

Where, in a value parameter, an argument can also be an expression that any calculation result satisfies a type requirement, not necessarily a variable.

Look at the following code example:

static void Main (string[] args)
        {program
            Pro = new program ();
            int nnum = ten;
            When the Add method is invoked, two arguments are passed, the first is the actual value,
            //The second nnum is a variable, but
            //int nresult = Pro is initialized when the method is passed in. ADD (nnum);
            int i = ten;
            int w = 20;
	    Here I * 2 and (W+10)/10 played the role of the argument
            int nresult = Pro. ADD (i * 2, (w + a)/ten);
            Console.WriteLine (Nresult);
        }

        The a,b here is called the formal parameter public
        int ADD (int a, int b)
        {return
            a + b;
        }
Here's a snippet of code that observes the parameters, the allocation and usage of the arguments in the managed heap and the managed stack

Class program
    {
        static void Main (string[] args)
        {program
            Pro = new program ();
            Rectangle Rectange = new Rectangle ();
            int myvalue = ten;

            The rectange,myvalue here are argument
            Pro. CalculateArea (Rectange, myvalue);//Call Method
        }

        //Here (Rectangle Rect,int value) is the formal parameter public
        void CalculateArea (Rectangle rect,int value)
        {
            rect. Length + = ten;
            Rect.width + +;
            Rect. Area = rect. Length * RECT.WIDTH;
            value++
        }
    }

    A rectangular class public class
    Rectangle
    {public
        int Length = ten;
        public int width =;
        public int area;
    }

① before the method is invoked: the system allocates space for the instance Rectange and value type of the rectangle class in the stack, where the reference type Rectange points to a Rectangle object instance in the heap, myvalue is a value type, so its value is in the managed stack. The demo diagram is as follows

At the beginning of the ② method call: The real arguments value is copied to the CalculateArea parameter, which rectange to the reference type, because a new reference--rect is copied, and the two references are now pointing to the same object, MyValue is a value type, so you can copy its value directly- Value The demo diagram is as follows:


In the ③ method invocation, change the Length field and width field of the object to which the reference refers, and add a value of 1


After the ④ method call, the parameter rect and value are ejected from the stack. MyValue is a value type, his value does not change (the parameter value is changed); Rectange is a reference type, and the modification to it is actually a modification to an object in the managed heap with an unmodified value.


To understand value-passing parameters, the main understanding of value types and reference types in the managed stack and the managed heap status. It's very easy to understand.


Original works, allow reprint, reprint, please be sure to hyperlink form to indicate the original source of the article, author information and this statement. Otherwise, legal liability will be held.

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.