C # Sharp Experience (vi)

Source: Internet
Author: User
Tags modifier modifiers requires

How to Speak

The method is also called a member function, which embodies the behavior of the class or object. Methods are also divided into static methods and instance methods. Static methods can manipulate only static fields, and instance methods can manipulate either the instance domain or the static domain-although this is not recommended, it can be useful in some special cases. The method also has the same 5 kinds of accessor modifiers--public,protected,internal,protected internal,private as the domain, and their meanings are as described earlier.

Method parameters

The parameter of the method is a place that deserves special attention. There are four types of parameter passes for the method: by value, pass (by reference), output parameter (by output), array parameter (by array). The pass value parameter requires no additional modifiers, the address parameter needs modifier ref, the output parameter requires a modifier out, and the array parameter requires a modifier params. If you change the value of a parameter during a method call, the parameters of the incoming method do not change after the method call is completed, but instead preserve the value of the original pass in. On the contrary, if the method call procedure changes the value of the parameter, then the parameters of the incoming method will change after the call completes. In fact, from the name we can clearly see the meaning of the two--the value parameter is passed is a copy of the call parameter, and the address parameter is passed the call parameter of the memory addresses, which point to the same storage location inside and outside the method. Look at the example below and its output:

using System;
class Test
{
    static void Swap(ref int x, ref int y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
    static void Swap(int x,int y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
    static void Main()
    {
        int i = 1, j = 2;
        Swap(ref i, ref j);
        Console.WriteLine("i = {0}, j = {1}", i, j);
        Swap(i,j);
        Console.WriteLine("i = {0}, j = {1}", i, j);
    }
}

The program executes output after compiling:

i = 2, j = 1
i = 2, j = 1

We can see clearly that the two commutative functions swap () have different invocation results because of the difference in parameters--the value and the address. Note The method invocation of the address parameter is added with the ref modifier either at the time of declaration or at the time of invocation.

Generally speaking, passing values does not change the value of parameters in some cases is wrong, let's look at the following example:

using System;
class Element
{
    public int Number=10;
}
class Test
{
    static void Change(Element s)
    {
        s.Number=100;
    }
    static void Main()
    {
        Element e=new Element();
        Console.WriteLine(e.Number);
        Change(e);
        Console.WriteLine(e.Number);
    }
}

The program executes output after compiling:

10
100

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.