성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
[정성태] Modules 창(Ctrl+Shift+U)을 띄워서, 해당 Op...
[정성태] 만드실 수 있습니다. 단지, Unity 엔진 내의 스크립트와 W...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# 7.3에서 개선된 문법 4개(Support == and != for tuples, Ref Reassignment, Constraints, Stackalloc initializers)</h1> <p> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# 7.3 (1) - 개선된 문법 4개(Support == and != for tuples, Ref Reassignment, Constraints, Stackalloc initializers) ; http://www.sysnet.pe.kr/2/0/11552 C# 7.3 (2) - 개선된 메서드 선택 규칙 3가지(Improved overload candidates) ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/11553'>http://www.sysnet.pe.kr/2/0/11553</a> C# 7.3 (3) - 자동 구현 속성에 특성 적용 가능(Attribute on backing field) ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/11554'>http://www.sysnet.pe.kr/2/0/11554</a> C# 7.3 (4) - 사용자 정의 타입에 fixed 적용 가능(Custom fixed) ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/11555'>http://www.sysnet.pe.kr/2/0/11555</a> C# 7.3 (5) - 구조체의 고정 크기를 갖는 fixed 배열 필드에 대한 직접 접근 가능(Indexing movable fixed buffers) ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/11556'>http://www.sysnet.pe.kr/2/0/11556</a> C# 7.3 (6) - blittable 제네릭 제약(blittable) ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/11558'>http://www.sysnet.pe.kr/2/0/11558</a> C# 7.3 (7) - 초기화 식에서 변수 사용 가능(expression variables in initializers) ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/11560'>http://www.sysnet.pe.kr/2/0/11560</a> </pre> <br /> <hr style='width: 50%' /><br /> <br /> 지난 5월 8일에 있었던 Visual Studio 15.7 업데이트부터 C# 7.3이 지원되기 시작했습니다. (<a target='tab' href='http://www.sysnet.pe.kr/2/0/11272'>적용 방법은 이렇게!</a>) 이전의 C# 7.2가 struct를 위주로 한 주요 업데이트가 있었다는 특징을 지녔던 반면, C# 7.3의 경우 특정 목적을 두지는 않고 편의성을 높인 업데이트가 주류를 이루고 있습니다. 아래의 문서에 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Language Feature Status ; <a target='tab' href='https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md'>https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md</a> </pre> <br /> C# 7.3으로 총 11개의 특징이 있는데 그중에서 아래의 글에서도 미리 소개한,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Microsoft Build 2018 - The future of C# 동영상 내용 정리 ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/11536'>http://www.sysnet.pe.kr/2/0/11536</a> </pre> <br /> 4개의 기능을 이 글에서 다시 간략하게 정리해 보겠습니다.<br /> <br /> <ul> <li>Support == and != for tuples</li> <li>Ref Reassignment</li> <li>Constraints</li> <li>Stackalloc initializers</li> </ul> <br /> <hr style='width: 50%' /><br /> <br /> <br /><div style='font-size: 12pt; font-family: Malgun Gothic, Consolas; color: #2211AA; text-align: left; font-weight: bold'>Support == and != for tuples</div> <br /> 우선, tuple 타입에 대해 ==, != 비교 연산자에 대한 지원이 추가되었습니다. 따라서, C# 7.2 이전에는 다음의 코드를 컴파일할 수 없지만,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; class Program { static void Main(string[] args) { var tuple = (13, "Kevin"); // C# 7.2까지 컴파일 오류 - Error CS0019 Operator '==' cannot be applied to operands of type '(int, string)' and '(int, string)' bool result1 = tuple == (13, "Winnie"); // C# 7.2까지 컴파일 오류 - Error CS0019 Operator '!=' cannot be applied to operands of type '(int, string)' and '(int, string)' bool result2 = tuple != (13, "Winnie"); } } </pre> <br /> C# 7.3부터는 정상적으로 컴파일할 수 있습니다. 비법은 간단합니다. C# 7.3 컴파일러는 위의 비교 코드를 개별 필드에 대한 비교 코드로 확장해서 처리합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > bool result1 = (tuple.Item1 == 13) && (tuple.Item2 == "Winnie"); bool result2 = (tuple.Item1 != 13) || (tuple.Item2 != "Winnie"); </pre> <br /> <hr style='width: 50%' /><br /> <br /> <br /><div style='font-size: 12pt; font-family: Malgun Gothic, Consolas; color: #2211AA; text-align: left; font-weight: bold'>Ref Reassignment</div> <br /> C# 7.0부터 추가된 ref 로컬 변수가 C# 7.2까지는 재할당을 허용하지 않았습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > static void Main(string[] args) { int a = 5; ref int b = ref a; // ref 로컬 변수 b int c = 6; // C# 7.2까지 컴파일 오류 - Error CS1073 Unexpected token 'ref' b = ref c; // 새롭게 변수 c에 대한 ref를 할당 } </pre> <br /> 하지만 C# 7.3 컴파일러부터 이를 허용합니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> <br /><div style='font-size: 12pt; font-family: Malgun Gothic, Consolas; color: #2211AA; text-align: left; font-weight: bold'>Constraints</div> <br /> 제네릭 관련한 where 제약에 Delegate, Enum과 unmanaged가 추가되었습니다. 따라서 다음과 같은 식의 제약을 추가하는 것이 가능합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > unsafe void M<D, E, T>(D d, E e, T* pointer) where D : Delegate where E: Enum where T: unmanaged { // ... [생략]... } </pre> <br /> C# 7.2 이하에서는 위의 코드를 컴파일하면 다음과 같은 오류가 발생했습니다.<br /> <br /> <ul> <li>Error CS0702 Constraint cannot be special class 'Delegate'</li> <li>Error CS0702 Constraint cannot be special class 'Enum'</li> <li>Error CS0246 The type or namespace name 'unmanaged' could not be found (are you missing a using directive or an assembly reference?)</li> </ul> <br /> <hr style='width: 50%' /><br /> <br /> <br /><div style='font-size: 12pt; font-family: Malgun Gothic, Consolas; color: #2211AA; text-align: left; font-weight: bold'>Stackalloc initializers</div> <br /> stackalloc은 C# 1.0부터 제공하던 예약어였지만, 배열 초기화 구문은 C# 7.3에 와서야 제공이 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > { Span<int> span1 = stackalloc <span style='color: blue; font-weight: bold'>int[5] { 1, 2, 3, 4, 5 };</span> Span<int> span2 = stackalloc <span style='color: blue; font-weight: bold'>int[] { 1, 2, 3, 4, 5 };</span> } { int *pArray1 = stackalloc <span style='color: blue; font-weight: bold'>int[3] { 1, 2, 3 }</span>; int *pArray2 = stackalloc <span style='color: blue; font-weight: bold'>int[] { 1, 2, 3 }</span>; } </pre> <br /> 아마도 C# 7.2의 Span 덕분에 스택 상의 배열에 대한 구문 사용이 늘어날 것을 예상하면서 편의상 추가한 것으로 보입니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> (<a target='tab' href='http://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1270&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
5147
(왼쪽의 숫자를 입력해야 합니다.)