Notice
Recent Posts
Recent Comments
Link
스토리지
[3.16] Dictionary 연습 1 본문
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study07
{
public class Product
{
private int ID;
private string name;
private int price;
public Product()
{
}
public Product(int ID, string name, int price)
{
this.ID = ID;
this.name = name;
this.price = price;
}
public int GetID()
{
return ID;
}
public string GetName()
{
return name;
}
public int GetPrice()
{
return price;
}
public override string ToString()
{
return String.Format("{0} {1} {2}억 메소", ID, name, price);
}
}
}
using System;
using System.Collections.Generic;
namespace Study07
{
public class App
{
public App()
{
//Dictionary<int, Product> 변수 선언
Dictionary<int, Product> dicProducts;
//컬렉션 인스턴스화
dicProducts = new Dictionary<int, Product>
{
//컬렉션 초기화 단순화 (컴파일러)
//Product 생성 (ID, 이름, 가격) + 요소 추가
{ 132, new Product(132, "제네시스 피스톨", 1900) },
{ 120, new Product(120, "아케인셰이드 스피어", 2700) },
{ 149, new Product(217, "해방된 카이세리움", 2100) }
};
//요소의 KEY로 검색
int searchKey = 120;
Console.WriteLine(dicProducts[searchKey]);
Console.WriteLine(dicProducts[132]);
Console.WriteLine(dicProducts[149]);
//foreach문으로 출력 (KeyValuePair<int, Product>)
foreach(KeyValuePair<int, Product> product in dicProducts)
{
Console.WriteLine("{0} : {1}", product.Key, product.Value);
}
}
}
}
Comments