Difference between ref and out, use of value type and reference type, refout

Source: Internet
Author: User

Difference between ref and out, use of value type and reference type, refout

I just understood the difference between ref and out today. It is only for my personal understanding. If there are differences, please kindly advise. Thank you.

First of all, I feel that ref and out are for the value type. I used to think that they are for the reference type. Let's look at the following code.

1. First, the result is I = 0; ints [0] = 0 I = 0; ints [0] = 100

2. After ints is passed in as a reference type, ints [0] is assigned a value to reference the address of the reference type to the 100 value stack,

3. When Value Type I is introduced to another method, assigning values does not change the original string.

4. ref is used to solve this problem, so that the value type can be the same as the reference type, and the value will change after passing in the method.

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Demo{    class Program    {        static void SomeFunction(int[] ints, int i)        {            ints[0] = 100;            i = 100;        }        static void Main(string[] args)        {            int i = 0;            int[] ints = { 0, 1, 2, 3, 4 };            Console.WriteLine("i=" + i);            Console.WriteLine("ints=" + ints[0]);            SomeFunction(ints, i);            Console.WriteLine("i=" + i);            Console.WriteLine("ints=" + ints[0]);            Console.ReadKey();        }    }}

  

5. Add the code to the ref result: I = 0; int [0] = 0; I = 100; ints [0] = 100

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace Demo
{
Class Program
{
Static void SomeFunction (int [] ints, ref int I)
{
Ints [0] = 100;
I = 100;
}
Static void Main (string [] args)
{
Int I = 0;
Int [] ints = {0, 1, 2, 3, 4 };
Console. WriteLine ("I =" + I );
Console. WriteLine ("ints =" + ints [0]);
SomeFunction (ints, ref I );
Console. WriteLine ("I =" + I );
Console. WriteLine ("ints =" + ints [0]);
Console. ReadKey ();
}
}
}

So I think that ref is a change to the value type, and like out, variables are initialized when passing parameters.

 

The out parameter does not need to be assigned a value when passing the parameter, but must be assigned a value in the called method. The out parameter that is not assigned is reported.

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace Demo
{
Class Program
{
Static void SomeFunction (int [] ints, ref int I)
{
Ints [0] = 100;
I = 100;
}
Static void Main (string [] args)
{
Int I = 0;
Int [] ints = {0, 1, 2, 3, 4 };
Console. WriteLine ("I =" + I );
Console. WriteLine ("ints =" + ints [0]);
SomeFunction (ints, ref I );
Console. WriteLine ("I =" + I );
Console. WriteLine ("ints =" + ints [0]);
Console. ReadKey ();
}
}
}

 

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.