본문 바로가기

유니티(Unity)

Array,List,Dictionary 참조 변경

using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		string[] arr = {"a","b","c","d"};
		foreach(string str in arr)
		{
			Console.Write(str);
		}
		Console.WriteLine("--------");
		string[] arr2 = {"d","e","f","g", "h", "i"};
		arr = arr2;
		foreach(string str in arr)
		{
			Console.Write(str);
		}
	}
}

결과 : abcd--------
defghi

using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		Dictionary<int, string> dict1 = new Dictionary<int, string>()
		{
			{1, "one"},
			{2, "two"},
			{3, "three"}
		};
		foreach(KeyValuePair<int, string> pair in dict1)
		{
			Console.Write(pair.Key);
			Console.Write(pair.Value);
		}
		Console.WriteLine("--------");
		Dictionary<int, string> dict2 = new Dictionary<int, string>()
		{
			{3, "threethree"},
			{4, "four"},
			{5, "five"},
			{6, "six"}
		};
		dict1 = dict2;
		foreach(KeyValuePair<int, string> pair in dict1)
		{
			Console.Write(pair.Key);
			Console.Write(pair.Value);
		}
	}
}

결과 : 1one2two3three--------
3threethree4four5five6six

using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		List<string> list = new List<string>(){"a","b","c","d"};// 
		foreach(string str in list)
		{
			Console.Write(str);
		}
		Console.WriteLine("--------");
		List<string> list2 = new List<string>(){"d","e","f","g", "h"};
		list = list2;
		foreach(string str in list)
		{
			Console.Write(str);
		}
	}
}

결과 :  abcd--------
defgh