성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
[정성태] Modules 창(Ctrl+Shift+U)을 띄워서, 해당 Op...
[정성태] 만드실 수 있습니다. 단지, Unity 엔진 내의 스크립트와 W...
[공진영] 안녕하세요 좋은글 감사합니다. 현재 제가 wpf로 관제 모...
[정성태] The Windows Registry Adventure #1: ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<span> <br /> 오늘 소개해 드리는 토픽은 다음과 같습니다.<br /> <br /> <pre class='code'> Automatically Implemented Properties - Visual Studio Orcas C# Compiler - C# 3.0 ; <a target='_tab' href='http://davidhayden.com/blog/dave/archive/2006/12/07/AutomaticallyImplementedPropertiesCSharpCompiler.aspx'>http://davidhayden.com/blog/dave/archive/2006/12/07/AutomaticallyImplementedPropertiesCSharpCompiler.aspx</a> </pre> <br /> 그 전에 다음의 토픽을 봐두시는 것도 좋을 듯 싶습니다. ^^<br /> <br /> <pre class='code'> 프로퍼티와 공용 필드에 대한 선택 ; <a target='_tab' href='/2/2/93'>http://www.sysnet.pe.kr/2/2/93</a> </pre> <br /> 정리해 보면, 예전 같으면 다음과 같이 선언하던 것을, <br /> <br /> <pre class='code'> private string name; public string Name { get { return this.name; } set { this.name = value; } } </pre> <br /> 이제는 C# 3.0 컴파일러의 지원으로 다음과 같이 줄여서 쓰는 것이 가능해 졌습니다.<br /> <br /> <pre class='code'> public string Name { <b>get; set;</b> } </pre> <br /> Access Modifier가 온다는 것만 제외한다면 인터페이스에서의 속성 선언하는 구문과 동일하다는 것을 알 수 있습니다. 조금 다른 점이 있다면, 반드시 get; set; 을 모두 포함하고 있어야 합니다. (업데이트 2021-01-27: C# 6.0부터 set을 제거한 readonly 표현이 가능해졌습니다.)<br /> <br /> 사용예는 다음과 같습니다.<br /> <br /> <pre class='code'> using System; using System.Linq; using System.Collections.Generic; namespace ConsoleApplication1 { public class Customer { <b>public string Test { get; set; }</b> // C# 3.0 <b>public int Age { get; }</b> // C# 6.0 <b>public bool HasJob { get; } = true;</b> // C# 6.0 public void DoFunc() { <b>this.Test</b> = "TEST"; } public Customer() { Age = 10; // read-only 속성은 생성자 내에서만 변경 가능 } } class Program { static void Main(string[] args) { Customer customer = new Customer(); customer.<b>Test</b> = "Test"; } } } </pre> <br /> .NET Reflector 로 열어봤더니 다음과 같이 되어 있는 것을 확인할 수 있었습니다.<br /> <br /> <pre class='code'> [CompilerGenerated] private string <b><>k__AutomaticallyGeneratedPropertyField0</b>; <b>public string Test</b> { [CompilerGenerated] get { return this.<b><>k__AutomaticallyGeneratedPropertyField0</b>; } [CompilerGenerated] set { <b>this.<>k__AutomaticallyGeneratedPropertyField0</b> = value; } } </pre> <br /> 낯이 익은 트릭이죠? 익명 메서드를 처리하던 것과 유사한 방식으로... 중복되지 않을 듯한 이름으로 된 내부 private 필드를 정의해 놓고 그것을 사용하고 있습니다.<br /> <br /> <br /><br /><hr /><span style='color: Maroon'>[이 토픽에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </span>
첨부파일
스팸 방지용 인증 번호
1356
(왼쪽의 숫자를 입력해야 합니다.)