아디봉의.net

[C#] getter, setter 의 사용 본문

C#

[C#] getter, setter 의 사용

아디봉 2012. 7. 25. 16:15

 

c#의 속성기능은 클래스의 데이터일부를 노출시켜서 사용 ,set 으로 설정되거 get 으로 반환되는 방식으로 분리시켜 사용

마치 일반적인 필드처럼 속성값이 읽고 쓰여지는 것을 확인할 수 있다. 속성을 사용하는 것이 필드를 사용하는 것보다 바람직함, 왜냐하면 해당 속성을 사용하는 클래스들을 일일이 변경하지 않고 get, set으로 변경가능 하기 때문

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            product1 ppd = new product1();
            ppd.category = "말순이";
            ppd.description = "말벌";
            ppd.name = "홍길동";
            ppd.price = "!111";
            ppd.productID =09203;
            /*
            product1 pd = new product1();
            pd.Name = "홍길동";
            pd.ProductID = 1245;
            pd.Description = "456789";
            pd.Category = "type";
            pd.Price = "10,000원";
            */

            Console.WriteLine("{0}", ppd.category);
            Console.WriteLine("{0}", ppd.productID);
            Console.WriteLine("{0}", ppd.description);
            Console.WriteLine("{0}", ppd.category);
            Console.WriteLine("{0}", ppd.price);
          
        }
    }

    class product1
    {

//자동으로 구현되는 속성
        public string name{get; set;}
        public int productID { get; set; }
        public string description { get; set; }
        public string price { get; set; }
        public string category { get; set; }

        /*
        public int ProductID
        {
            get { return productID; }
            set { productID = value; }
        }
        public string Description
        {
            get { return description; }
            set { description = value; }
        }
        public string Price
        {
            get { return price; }
            set { price = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string Category
        {
            get { return category; }
            set { category = value; }
        }
          */
    }
}

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

C# com+ 프로젝트 생성과 사용  (0) 2012.08.16
[C#] 인터페이스 정리  (0) 2012.07.25
[펌] visual 2010 도움되는기능  (0) 2012.07.12
c# 제네릭(Generic) 완벽정리해보쟈!  (0) 2012.07.11
[펌] 01.overview of Attributes  (0) 2012.07.09