teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

不要な画像削除。

2018/02/20 15:47

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -76,6 +76,4 @@
76
76
  }
77
77
  }
78
78
  }
79
- ```
79
+ ```
80
- ChangeRef()は下記のイメージで合っていますか?
81
- ![イメージ説明](5c1a5154890ec287167796d9e4e192c7.png)

1

追記

2018/02/20 15:47

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -28,4 +28,54 @@
28
28
  void DoNotRef (List<string> list) {
29
29
  list.Add ("2");
30
30
  }
31
- ```
31
+ ```
32
+
33
+ ### 追記。
34
+
35
+ ChangeNoRef()で、l = new List<string>();したとき、何が起こっているのか分かりません。
36
+
37
+ ```C#
38
+ using System.Collections;
39
+ using System.Collections.Generic;
40
+ using UnityEngine;
41
+
42
+ public class Sample : MonoBehaviour {
43
+
44
+ // Use this for initialization
45
+ void Start () {
46
+ var list = new List<string>();
47
+ list.Add("Hello");
48
+ DebugList (list); //Hello
49
+
50
+ Debug.Log("変更します");
51
+ ChangeNoRef (list);
52
+ DebugList (list); //Hello
53
+
54
+
55
+ Debug.Log("変更します");
56
+ ChangeRef(ref list);
57
+ DebugList (list); //World
58
+ }
59
+
60
+ static void ChangeNoRef(List<string> l)
61
+ {
62
+ l = new List<string>(); //この行をコメントアウトしたら、Hello, Worldになる。
63
+ l.Add("World");
64
+ }
65
+
66
+
67
+ static void ChangeRef(ref List<string> l)
68
+ {
69
+ l = new List<string>();
70
+ l.Add("World");
71
+ }
72
+
73
+ void DebugList(List<string> l){
74
+ foreach (string s in l) {
75
+ Debug.Log (s);
76
+ }
77
+ }
78
+ }
79
+ ```
80
+ ChangeRef()は下記のイメージで合っていますか?
81
+ ![イメージ説明](5c1a5154890ec287167796d9e4e192c7.png)