컬렉션(C#)

Unity3D/C# 2019. 3. 28. 23:14
반응형

컬렉션 (C#)
관련개체의 그룹을 만들고 관리 
개체를 그룹화 하는 방법에는 개체 배열을 만들거나 개체 컬렉션을 만드는 두가지 방법이 있음.

배열: 고정된 개수의 강력한 형식 개체를 만들고 작업 하는데 유용 
https://docs.microsoft.com/ko-kr/dotnet/csharp/programming-guide/arrays/index

컬렉션 : 개체그룹에 대해 작업하는 보다 유연한 방법을 제공
배열과달리 변경되는 작업에서 개체 그룹이 동적으로 확장되거나 축소될수 있음.
일부 컬렉션의 경우 키를 사용하여 개체를 신속하게 검색할 수 있도록 컬렉션에 추가 하는 모든 개체에 키를 할당할수 있음.
컬렉션은 클래스이므로 해당 컬렉션에 요소를 추가 하려면 먼저 클래스 인스턴스를 선언 해야 함.
컬렉션에 단일 데이터 형식의 요소만 포함된 경우 System.Collections.Generic 네임스페이스의 클래스중 하나를 사용할 수 있다.
제네릭 컬렉션은 다른 데이터 형식을 추가 할수 없도록 형식 안전성을 적용한다.
제네릭 컬렉션에서 요소를 검색 하는 경우 해당 데이터 형식을 결정하거나 변환할 필요가 없다.

제네릭 : https://docs.microsoft.com/ko-kr/dotnet/csharp/programming-guide/generics/

컬렉션의 종류 
- System.Collections.Generic 클래스 
- System.Collections.Concurrent 클래스 
- System.Collections 클래스 

키/값 쌍의 컬렉션 구현 
Linq를 사용하여 컬렉션에 엑세스 
컬렉션 정렬 
사용자 지정 컬렉션 정의
반복기 

 

 

 

Program.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
 
namespace study03
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var app = new App();
            app.Run();
        }
    }
}
 
 
 



App.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
 
namespace study03
{
    public class App
    {
        public App()
        { 
            //생성자 
        }
 
        public void Run()
        {
            //가방에 넣을 초기 아이템 
            Item[] items = new Item[3]
                {
                    new Item("가죽장갑", Item.eItemType.Armor, 1),
                    new Item("혈마도"Item.eItemType.Weapon, 1),
                    new Item("체력포션"Item.eItemType.Potion, 10)
                };
 
            Inventory inventory = new Inventory(items);
            foreach(Item item in inventory)
            {
                Console.WriteLine("{0} {1} {2}"item.Name, item.itemType, item.stack);
            }
 
            Console.WriteLine();
 
            var enumerator = inventory.GetEnumerator();
            while (enumerator.MoveNext())
            {
                var item = enumerator.Current;
                Console.WriteLine("{0} {1} {2}"item.Name, item.itemType, item.stack);
            }
 
        }
    }
}
 
 
 

 

 

 

Item.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
 
namespace study03
{
    public class Item
    {
        public enum eItemType
        { 
            None = 0,
            Weapon = 1,
            Armor = 2,
            Accessory = 4,
            Potion = 8
        }
 
        public string Name { get; private set; } 
        public eItemType itemType { get; private set; }
        public int stack { get; private set; }
 
        public Item(string name, eItemType itemType, int stack)
        {
            this.Name = name;
            this.itemType = itemType;
            this.stack = stack;
        }
    }
}
 
 
 

 

 

Inventory.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Collections;
 
namespace study03
{
    public class Inventory : IEnumerable
    {
        private Item[] items;
 
        public Inventory(Item[] items)
        {
            this.items = items;
        }
 
        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)GetEnumerator();
        }
 
        public ItemEnumerator GetEnumerator()
        {
            return new ItemEnumerator(this.items);
        }
    }
}
 
 
 

 

 

ItemEnumerator.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections;
 
namespace study03
{
    public class ItemEnumerator : IEnumerator
    {
        public Item[] items;
        private int position = -1;
 
        public ItemEnumerator(Item[] items)
        {
            this.items = items;
        }
 
        public bool MoveNext()
        {
            this.position++;
            return (this.position < this.items.Length);
        }
 
        public void Reset()
        {
            this.position = -1;
        }
 
        object IEnumerator.Current
        {
            get 
            {
                return Current;
            }
        }
 
        public Item Current
        {
            get
            {
                try 
                {
                    return this.items[this.position];
                }
                catch
                {
                    throw new InvalidOperationException();
                }
            }
        }
    }
}
 
 
 
반응형
: