-
03/17 List<T> 변수 선언, 인스턴스 생성, 값 할당, 출력 복습 2C#/수업내용 2021. 3. 17. 11:01
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study06 { class Program { static void Main(string[] args) { Console.WriteLine("Main"); new App(); } } }
App.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study06 { public class App { public App() { Console.WriteLine("App"); //List<Item> 변수 선언 List<Item> listItem; //List<Item> 인스턴스 및 변수에 할당 listItem = new List<Item>(); //Item 객체 생성 Item item1 = new Item(10, "장검"); Item item2 = new Item(11, "단검"); Item item3 = new Item(12, "창"); Item item4 = new Item(13, "활"); Item item5 = new Item(14, "석궁"); //List<Item> 요소에 값 추가 listItem.Add(item1); listItem.Add(item2); listItem.Add(item3); listItem.Add(item4); listItem.Add(item5); //List<Item> 의 요소의 수 출력 Console.WriteLine("List<Item> 의 요소의 수 출력 : " + listItem.Count); //foreach문을 사용해 List<Item>의 요소 출력 (아이템 ID, 아이템 이름) Console.WriteLine("\nforeach문을 사용해 List<Item>의 요소 출력 (아이템 ID, 아이템 이름)"); foreach(Item item in listItem) { Console.WriteLine("({0}, {1})", item.GetId(), item.GetName()); } } } }
Item.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study06 { public class Item { private int id; private string name; public Item(int id, string name) { this.id = id; this.name = name; } public int GetId() { return this.id; } public string GetName() { return this.name; } } }
'C# > 수업내용' 카테고리의 다른 글
03/17 인터페이스연습 2 (0) 2021.03.17 03/17 인터페이스 연습 1 (0) 2021.03.17 03/17 배열 변수 선언, 인스턴스 생성, 값 할당, 출력 복습 2 (0) 2021.03.17 03/17 Dictionary 변수 선언, 인스턴스 생성, 값 할당, 출력 복습 (0) 2021.03.17 03/17 List<T> 변수 선언, 인스턴스 생성, 값 할당, 출력 복습 (0) 2021.03.17