본문 바로가기

[C#] List

@Xenawn2025. 2. 7. 21:44
반응형
namespace Collection
{
    internal class Program
    {
        public class Weapon
        {
            string name;
            int hp;
            int mp;

            public Weapon(string name, int hp, int mp)
            {
                this.name = name;  
                this.hp = hp;
                this.mp = mp;
            }

            public void PrintInfo()
            {
                Console.WriteLine($"{name}이다. 내 체력은{hp}이고 내 마력은{mp}이다");
            }
        }
        static void Main(string[] args)
        {
            List<Weapon> list = new List<Weapon>();

            Weapon weapon = new Weapon("밥버거", 10, 20);

            list.Add(weapon);

            list[0].PrintInfo();

            List<Weapon> weaponList = new List<Weapon>
            {
                new Weapon("안녕하시오",10,20),
                new Weapon("밥냄새",10,30),
                new Weapon("햄버거",30,50)
            };
            // 반복문 사용해보기

            for(int i =0; i<weaponList.Count; i++)
            {
                weaponList[i].PrintInfo();
            }
        }
    }
}

 

 

오늘은 리스트의 사용법을 알아봤는데

리스트는 Count를 쓴다 

Length는 배열

반응형

'Language > C#' 카테고리의 다른 글

[C#] 얕은 복사  (0) 2025.02.10
[C#] DeepCopy  (0) 2025.02.10
[C#] Convert.ToInt32() vs int.Parse()  (0) 2025.02.06
[C#] 상속  (0) 2025.02.06
[C#] Text RPG  (0) 2025.02.05
Xenawn
@Xenawn :: Xenawn

제넌 게임개발 블로그

공감하셨다면 ❤️ 구독도 환영합니다! 🤗

목차