Understand. Difference between method signature with out, ref and common method signature in net

Source: Internet
Author: User
Tags modifier

A new colleague asked me today. NET, what is the difference between a method signature with out, a ref, and a common method signature? I think we can illustrate some key points from the following examples.

One, ref/out modifier description

The instructions for using the Ref/out modifier are detailed on MSDN with the following address:

HTTP://MSDN.MICROSOFT.COM/EN-US/LIBRARY/T3C3BFHX (vs.80). aspx.

Second, through the IL Code observation ref/out decorated method signature (in the case of value types)

1. Sample code:

using System;

namespace ConsoleMain
{
    class Program
    {
        static void Main()
        {
            Int32 p ;

            TestRef(out p);                             //①

            //TestRef(ref p)                        //②

            TestRef(p);                         //③

            Console.ReadKey();
        }

        static void TestRef(Int32 para)                 //④
        {
            para = 1;
        }

        static void TestRef(out Int32 para)                //⑤
        {
            para = 2;
        }

        /*static void TestRef(ref Int32 para)                //⑥
        {
            Para3 =  3;
        } */
    }
}

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.