The difference between ref and out, the use of value types and reference types

Source: Internet
Author: User

Today I just understand that the difference between ref and out is limited to personal understanding. Please enlighten me if you have different.

First I feel that ref and out are for value types, which was previously thought to be for reference types see the following section of code

1. First results i=0;ints[0]=0 i=0;ints[0]=100

2.ints as a reference type after passing the method, the ints[0] is assigned, is to refer to the address of the type of the 100 is worth the heap,

3. When the value type I is introduced to another method, the assignment does not change the original string

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

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] = n;            i = +;        }        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 code to 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, the variables are initialized when the arguments are passed.

The out-of-the-box does not have to be assigned when passing parameters, but it must be assigned in the calling method, which will report out parameters that are not assigned.

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 ();
}
}
}

The difference between ref and out, the use of value types and reference types

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.