質問

したがって、foreachループを使用して反復し、リストから反復されたオブジェクトの引数を取る関数を内部に持っている場合、その値を異なる値に設定するとします。 outまたはrefを使用する必要がないのはなぜですか? outまたはrefを使用しなかった場合にのみ値で渡されると思いました。

リストを反復処理し、実際に参照渡しされたオブジェクトを渡す場合のようです。次の例を検討してください。

class Program
    {
        static void Main(string[] args)
        {

            List<Foo> list = new List<Foo>();
            list.Add(new Foo() { Bar = "1" });
            list.Add(new Foo() { Bar = "2" });



            foreach (var f in list)
            {
                Foo f2 = f; 
                Console.WriteLine("SetFoo Pre: " + f2.Bar);
                SetFoo(f2);
                Console.WriteLine("SetFoo Post: " + f2.Bar);

                Console.WriteLine("SetFooRef Pre: " + f2.Bar);
                SetFooRef(ref f2);
                Console.WriteLine("SetFooRef Post: " + f2.Bar);
                Console.WriteLine("");
            }




            Console.WriteLine("");

            int i = 0;
            // Not using ref keyword
            Console.WriteLine("SetI Pre: " + i);
            SetI(i);
            Console.WriteLine("SetI Post: " + i);

            // Using ref keyword
            Console.WriteLine("SetRefI Pre: " + i);
            SetRefI(ref i);
            Console.WriteLine("SetRefI Post: " + i);
        }


        private static void SetRefI(ref int i)
        {
            i = 3;
            Console.WriteLine("SetRefI Inside: " + i);
        }

        private static void SetI(int i)
        {
            i = 2;
            Console.WriteLine("SetI Inside: " + i);
        }

        private static void SetFooRef(ref Foo f)
        {
            f.Bar = String.Format("{0} :: {1}", f.Bar, "WithRef");
            Console.WriteLine("SetFooRef Inside: " + f.Bar);
        }

        private static void SetFoo(Foo f)
        {
            f.Bar = String.Format("{0} :: {1}", f.Bar, "WithoutRef");
            Console.WriteLine("SetFoo Inside: " + f.Bar);
        }
    }


    class Foo
    {
        public string Bar { get; set; }
    }

出力:

  

SetFoo Pre:1 SetFoo Inside:1 ::
  WithoutRef SetFoo投稿:1 WithoutRef
  SetFoo Pre:1 :: WithoutRef SetFoo
  内部:1 :: WithoutRef :: WithRef
  SetFoo投稿:1 WithoutRef :: WithRef

     

SetFoo Pre:2 SetFoo Inside:2 ::
  WithoutRef SetFoo投稿:2 WithoutRef
  SetFoo Pre:2 :: WithoutRef SetFoo
  内部:2 :: WithoutRef :: WithRef
  SetFoo投稿:2 WithoutRef :: WithRef

     

SetI Pre:0 SetI Inside:2 SetIPost:0

     

SetRefI Pre:0 SetRefI Inside:3
  SetRefI投稿:3

整数の例で参照を理解していますが、上記のFooオブジェクトの反復の例では理解できません。

ありがとう!

役に立ちましたか?

解決

参照は値で渡されます。そのため、メソッドはオブジェクトの内容を変更できますが、変数が参照するオブジェクトを変更することはできません。

参照タイプと値タイプ

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top