성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
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# 11 - 정적 메서드에 대한 delegate 처리 시 cache 적용</h1> <p> 자, 이것으로 C# 11의 마지막 글이 되겠습니다. ^^ 그런 만큼, 부담 없는 내용으로 가보겠습니다. 이번 기능은 딱히 외적으로 바뀌는 것이 아닌, 그냥 기존 소스 코드를 컴파일하면 자동으로 적용되는 부분입니다.<br /> <br /> 제목에서 이미 눈치채셨겠지만, 기존 C# 컴파일러는 static 메서드에 대한 delegate를 처리할 때,<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; public class Test { public static void Main() { TestMethod(); } <span style='color: blue; font-weight: bold'>private static void TestMethod() { Action<string> action = Output; action("Hello World!"); }</span> static void Output(string text) { Console.WriteLine(text); } } </pre> <br /> 다음과 같은 식으로 풀어냅니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // ...[생략]... private static void TestMethod() { Action<string> action = new Action<string>(Test.Output); action("Hello World!"); } // ...[생략]... </pre> <br /> 사실 딱히 이상한 것은 없습니다. 단지, Test.Output 메서드를 받는 delegate 인스턴스(위의 경우 Action)을 매번 생성한다는 부담이 있는 정도입니다. 만약 저 TestMethod가 자주 불리는 상황에서 성능을 개선하고 싶다면, 그러니까, 가령 GC Heap 할당을 줄이고 싶다면 다음과 같이 cache 처리를 하면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > <span style='color: blue; font-weight: bold'>static Action<string>? _action;</span> private static void TestMethod() { <span style='color: blue; font-weight: bold'>if (_action == null) { _action = Output; }</span> _action("Hello World!"); } </pre> <br /> 바로 저 과정! 저것을 C# 11 컴파일러부터는 알아서 해주겠다는 것입니다. 그래서 첫 번째 예제 코드를 C# 11로 빌드하면 이렇게 바뀌어서 컴파일이 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > private static void TestMethod() { Action<string> action2; if ((action2 = <span style='color: blue; font-weight: bold'>Test.<>O.<0>__Output</span>) == null) { action2 = (<span style='color: blue; font-weight: bold'>Test.<>O.<0>__Output</span> = new Action<string>(Test.Output)); } Action<string> action = action2; action("Hello World!"); } </pre> <br /> 문법적으로 바뀐 것은 없고, 알아서 해주는 거니까 말 그대로 "Free Lunch"를 즐기시면 됩니다. ^^<br /> <br /> <hr style='width: 50%' /><br /> <br /> 그나저나, 코드가 좀 낯이 익죠? ^^ 실제로 dynamic을 처리할 때도,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# 4.0 - dynamic 키워드 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/766'>https://www.sysnet.pe.kr/2/0/766</a> </pre> <br /> 저렇게 cache 용도를 두는 것과 유사합니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> (이것으로 C# 11의 모든 기능을 살펴봤고, <a target='tab' href='https://www.sysnet.pe.kr/2/0/13338'>C# 12로 넘어가실 수</a> 있습니다. ^^) <br /> <br /> <hr style='width: 50%' /><br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# 11 - 인터페이스 내에 정적 추상 메서드 정의 가능 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4436'>Static Abstract Members In Interfaces C# 10 Preview</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12814'>https://www.sysnet.pe.kr/2/0/12814</a> C# 11 - 제네릭 타입의 특성 적용 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#generic-attributes'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/124'>Generic attributes</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12839'>https://www.sysnet.pe.kr/2/0/12839</a> C# 11 - 사용자 정의 checked 연산자 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/checked-user-defined-operators.md'>Checked user-defined operators</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13099'>https://www.sysnet.pe.kr/2/0/13099</a> C# 11 - shift 연산자 재정의에 대한 제약 완화 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4666'>Relaxing Shift Operator</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13100'>https://www.sysnet.pe.kr/2/0/13100</a> C# 11 - IntPtr/UIntPtr과 nint/unint의 통합 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#numeric-intptr-and-uintptr'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/numeric-intptr.md'>Numeric IntPtr</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13111'>https://www.sysnet.pe.kr/2/0/13111</a> C# 11 - 새로운 연산자 ">>>" (Unsigned Right Shift) (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4682'>Unsigned right shift operator</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13110'>https://www.sysnet.pe.kr/2/0/13110</a> C# 11 - 원시 문자열 리터럴 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw-string-literals'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/raw-string-literal.md'>raw string literals</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13085'>https://www.sysnet.pe.kr/2/0/13085</a> C# 11 - 문자열 보간 개선 2가지 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#newlines-in-string-interpolations'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4935'>Allow new-lines in all interpolations</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13086'>https://www.sysnet.pe.kr/2/0/13086</a> C# 11 - 목록 패턴 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/list-patterns.md'>List patterns</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13112'>https://www.sysnet.pe.kr/2/0/13112</a> C# 11 - Span 타입에 대한 패턴 매칭 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#pattern-match-spanchar-or-readonlyspanchar-on-a-constant-string'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/1881'>Pattern matching on ReadOnlySpan<char></a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13113'>https://www.sysnet.pe.kr/2/0/13113</a> C# 11 - Utf8 문자열 리터럴 지원 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/utf8-string-literals.md'>Utf8 Strings Literals</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13096'>https://www.sysnet.pe.kr/2/0/13096</a> C# 11 - ref 필드 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/low-level-struct-improvements.md'>ref fields</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13015'>https://www.sysnet.pe.kr/2/0/13015</a> C# 11 - 파일 범위 내에서 유효한 타입 정의 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/issues/6011'>File-local types</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13117'>https://www.sysnet.pe.kr/2/0/13117</a> C# 11 - 메서드 매개 변수에 대한 nameof 지원 (<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#extended-nameof-scope'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/373'>nameof(parameter)</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13122'>https://www.sysnet.pe.kr/2/0/13122</a> C# 11 - 멤버(속성/필드)에 지정할 수 있는 required 예약어 추가 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/required-members.md'>Required members</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13123'>https://www.sysnet.pe.kr/2/0/13123</a> C# 11 - 구조체 필드의 자동 초기화 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/auto-default-structs.md'>auto-default structs</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13125'>https://www.sysnet.pe.kr/2/0/13125</a> C# 11 - 정적 메서드에 대한 delegate 처리 시 cache 적용 (공식 문서, <a target='tab' href='https://github.com/dotnet/roslyn/issues/5835'>Cache delegates for static method group</a>) ; https://www.sysnet.pe.kr/2/0/13126 Language Feature Status ; <a target='tab' href='https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md'>https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md</a> </pre> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
2695
(왼쪽의 숫자를 입력해야 합니다.)