C Sharp(C#) : 속성 (property)
MSDN saids... (https://msdn.microsoft.com/ko-kr/library/x9fsa0sw.aspx)
속성은 전용 필드의 값을 읽거나 쓰거나 계산하는 유연한 메커니즘을 제공하는 멤버입니다.
공용 데이터 멤버인 것처럼 속성을 사용할 수 있지만, 실제로 접근자라는 특수 메서드입니다.
나는 자바에서의 VO 아님 DTO 개념으로 접근하며 이해를 했다.
C#의 속성(property)라는 이 특수 메소드는 자바의 getter와 setter개념이 그대로 적용되어서 set과 get 접근자가 들어있다.
간단한 코드를 구현하자면 get과 set을 띡 쓰고 마침표를 찍어도 된다는 점이 매력인 것 같다.
속성은 get과 set 둘 중 하나가 생략 되어도 된다.
단, 맨 위의 1줄짜리 속성처럼 자동구현 속성에서는 get과 set이 둘 다 있어야 한다.
1 2 | private string name; public string Name { get; set; } | cs |
위의 저 코딩을 풀게 되면 이렇다.
1 2 3 4 5 6 | private string name; public string Name //property { get { return name; } set { name = value; } } | cs |
물론 get과 set접근자 안에다가 조건을 지정해서 변수를 지정해도 된다.
1 2 3 4 5 6 7 8 9 | private int age=0; public int Age { get { return age; } set { if (value > 0) age = value; } } | cs |
보안을 위한 Collection읽기전용 속성(Property) 설정하기
출처 : http://www.csharpstudy.com/Mistake/Article/17
OtherWorld 클래스가 호출되는 순간 생성자가 _keys라는 필드값을 설정하게 하고,
OtherWorld클래스 안의 속성을 이용하여 데이터를 읽어오는 방식으로 두가지 속성과 두가지 접근한정자를 이용하여 실험 해 봤다.
속성1. IEnumerable<int> : line 21
속성2. ReadOnlyCollection<int> : line 26
접근한정자1. private : line 12
접근한정자2. protected : line 13
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections.ObjectModel; namespace ConsoleStudy { class OtherWorld { //private List<int> _keys = new List<int>(); protected List<int> _keys = new List<int>(); public OtherWorld() { _keys.Add(111); _keys.Add(222); } public IEnumerable<int> Keys { get { return _keys; } } public ReadOnlyCollection<int> Keys2 { get { var ro = new ReadOnlyCollection<int>(_keys); return ro; } } } class ReadOnlyPropertyStudy : OtherWorld { static void Main() { ReadOnlyPropertyStudy mineclassinstance = new ReadOnlyPropertyStudy(); OtherWorld otherclassinstance = new OtherWorld(); /* TEST 1 */ List<int> mainkey = new List<int>(); mainkey.Add(153); /* 1. OtherWorld._keys 접근한정자가 'protected'라면 작동 1-2. 그러나 _keys의 형식이 OtherWorld형식이라면 오류('OtherWorld' 형식의 한정자를 통해 보호된 멤버 'OtherWorld._keys'에 액세스할 수 없습니다. 한정자는 'ReadOnlyPropertyStudy' 형식이거나 여기에서 파생된 형식이어야 합니다.) 1-3. 해당 클래스에서 OtherWorld를 상속받지 못한다면 작동안된다. // 2. OtherWorld._keys 접근한정자가 'private'라면 작동안됨(보호 수준 때문에 'OtherWorld._keys'에 액세스할 수 없습니다.) */ otherclassinstance._keys = mainkey; /* TEST 2 */ // 오류([]을 사용하는 인덱싱을 'IEnumerable<int>' 형식의 식에 적용할 수 없습니다.) - IEnumerable은 인덱서를 지원안하기 때문. Console.WriteLine(otherclassinstance.Keys[0]); /* TEST 3 */ // 인덱싱 지원하는 ReadOnlyCollection으로 Collection.List의 내용을 읽음 Console.WriteLine(otherclassinstance.Keys2[0]); Console.WriteLine(otherclassinstance.Keys2[1]); /* TEST 4 */ /* 1. OtherWorld._keys 접근한정자가 'protected'라면 작동 1-2. 그러나 _keys의 형식이 OtherWorld형식이라면 오류('OtherWorld' 형식의 한정자를 통해 보호된 멤버 'OtherWorld._keys'에 액세스할 수 없습니다. 한정자는 'ReadOnlyPropertyStudy' 형식이거나 여기에서 파생된 형식이어야 합니다.) 1-3. 해당 클래스에서 OtherWorld를 상속받지 못한다면 작동안된다. // 2. OtherWorld._keys 접근한정자가 'private'라면 작동안됨(보호 수준 때문에 'OtherWorld._keys'에 액세스할 수 없습니다.) */ Console.WriteLine(mineclassinstance._keys[0]); } } } | cs |
'C#' 카테고리의 다른 글
C#의 뼈대 : namespace,class,struct,interface,delegate,enum (0) | 2017.06.04 |
---|---|
C# 개념 : 실행순서 , CLR과 클래스 라이브러리 (0) | 2017.06.04 |