성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
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# - 조건 연산자(?:)를 사용하는 경우 달라지는 메서드 선택 사례</h1> <p> 이번 C# 9.0의 "target-typed conditional expression" 제안 문서를 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Target-Typed Conditional Expression ; <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/target-typed-conditional-expression'>https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/target-typed-conditional-expression</a> </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;' > using System; class Program { static void Main(string[] args) { <span style='color: blue; font-weight: bold'>M(1); M(2);</span> } static void M(<span style='color: blue; font-weight: bold'>short n</span>) { Console.WriteLine("Short"); } static void M(<span style='color: blue; font-weight: bold'>long n</span>) { Console.WriteLine("Long"); } } /* 출력 결과 <span style='color: blue; font-weight: bold'>Short Short</span> */ </pre> <br /> 3항 연산자로 다루는 경우가 다르다는 점입니다.<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) { M(<span style='color: blue; font-weight: bold'>(args.Length == 0) ? 1 : 2</span>); M(<span style='color: blue; font-weight: bold'>(args.Length == 1) ? 1 : 2</span>); } static void M(<span style='color: blue; font-weight: bold'>short n</span>) { Console.WriteLine("Short"); } static void M(<span style='color: blue; font-weight: bold'>long n</span>) { Console.WriteLine("Long"); } } /* 출력 결과 <span style='color: blue; font-weight: bold'>Long Long</span> */ </pre> <br /> <hr style='width: 50%' /><br /> <br /> 이에 대해 정확히 파악하려면, 언어 명세를 봐야 합니다. 실제로 아래의 문서를 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Constant expressions ; <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#constant-expressions'>https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#constant-expressions</a> </pre> <br /> 명확하게 "Constant expressions"에 대해,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > constant_expression : expression ; </pre> <br /> 숫자형의 범위에 따라 암시적인 형변환을 허용한다고 명시하고 있습니다.<br /> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> <span style='color: blue; font-weight: bold'>An implicit constant expression conversion</span> (Implicit constant expression conversions) <span style='color: blue; font-weight: bold'>permits</span> a constant expression of type <span style='color: blue; font-weight: bold'>int to be converted to</span> sbyte, byte, <span style='color: blue; font-weight: bold'>short</span>, ushort, uint, or ulong, <span style='color: blue; font-weight: bold'>provided the value of the constant expression is within the range of the destination type.</span> </div><br /> <br /> 즉, constant_expression인 경우 -128 ~ 127 범위의 숫자 리터럴이면 그 범위에 따라 short로 암시적 형변환을 허용하는 것입니다. 반면, 예제로 들었던 조건 연산자 식은 컴파일 타임에 식 자체가 constant_expression으로 표현되는 것은 아니므로 '범위에 따른 암시적 형변환'의 혜택을 받지 못합니다.<br /> <br /> 그런데, 재미있는 것은 "<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#constant-expressions'>Constant expressions</a>" 문서에 보면 다음의 대상이 constant_expression으로 허용된다고 하는데,<br /> <br /> <ul> <li>Literals (including the null literal).</li> <li>References to const members of class and struct types.</li> <li>References to members of enumeration types.</li> <li>References to const parameters or local variables</li> <li>Parenthesized sub-expressions, which are themselves constant expressions.</li> <li>Cast expressions, provided the target type is one of the types listed above.</li> <li>checked and unchecked expressions</li> <li>Default value expressions</li> <li>Nameof expressions</li> <li>The predefined +, -, !, and ~ unary operators.</li> <li>The predefined +, -, *, /, %, <<, >>, &, |, ^, &&, ||, ==, !=, <, >, <=, and >= binary operators, provided each operand is of a type listed above.</li> <li><span style='color: blue; font-weight: bold'>The ?: conditional operator.</span></li> </ul> <br /> 마지막에 조건 연산자가 나옵니다. 따라서, 메서드를 다음과 같이 호출하면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > M((3 == 4) ? 1 : 2); // M(short) </pre> <br /> 컴파일 시점에 조건 연산자 식의 값이 결정되므로 constant_expression으로 평가받게 되고 결국 M(short) 버전의 메서드가 호출됩니다.<br /> <br /> 여기서 유의할 것은, 숫자 리터럴에 대한 constant_expression의 암시적 형변환이 대상 타입으로 int가 없을 때 발생하는 것이지, 그것 자체가 값의 범위에 따라 평가받는 것은 아니라는 점입니다.<br /> <br /> 즉, 원래 숫자 리터럴의 타입은 Int32(범위를 넘어서면 Int64)입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Console.WriteLine("1 == " + 1.GetType().FullName); // System.Int32 Console.WriteLine("21474836473 == " + 21474836473.GetType().FullName); // System.Int64 var tenaryResult = (3 == 4) ? 1 : 2; Console.WriteLine(tenaryResult.GetType().FullName); // System.Int32 </pre> <br /> 그래서 만약 M(int)에 해당하는 메서드가 있었다면 M(1)로 호출해도 constant_expression의 암시적 형변환 단계를 거칠 필요 없이 M(int) 메서드가 선택이 됩니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 그런데, 재미있는 경우가 하나 더 있습니다. C# 8.0에서 나온 새로운 switch expression은,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > { bool result = args.Length == 1; M(result switch { true => 1, false => 2 }); // calls M(short) } </pre> <br /> 코딩 결과로 보면 조건 연산자와 동일한 역할을 하지만 short 버전의 메서드가 선택된다는 것입니다. 이것 역시 언어 명세를 보면 이에 대한 설명이 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Switch Expression ; <a target='tab' href='https://github.com/dotnet/csharplang/blob/a17f4c8ba82ed19fdad8b9f86ac151a443c89b08/proposals/csharp-8.0/patterns.md#switch-expression'>https://github.com/dotnet/csharplang/blob/a17f4c8ba82ed19fdad8b9f86ac151a443c89b08/proposals/csharp-8.0/patterns.md#switch-expression</a> </pre> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> <span style='color: blue; font-weight: bold'>The type of the switch_expression is the best common type of</span> the expressions appearing to <span style='color: blue; font-weight: bold'>the right of the => tokens</span> of the switch_expression_arms if such a type exists and the expression <span style='color: blue; font-weight: bold'>in every arm of the switch expression</span> can be <span style='color: blue; font-weight: bold'>implicitly converted to that type</span>. </div><br /> <br /> 즉, switch 식의 경우에는 모든 조건의 우측 operand를 기준으로 평가를 진행해 암시적 형변환 유무를 결정한다는 것입니다. 아마도 C# 언어 개발자들은 조건 연산자에 대해서도 2항과 3항 operand를 모두 평가해 암시적 형변환을 결정하는 것도 가능했을 것입니다. 하지만, 최초 C# 1.0 버전에서 그 작업을 진행하지 않았었고, 이후 버전 업이 되면서 그것을 switch 식처럼 모두 평가해 타입을 결정하는 것으로 변경했다면 하위 호환성이 깨지는 이유로 그대로 두었을 가능성이 큽니다. (어느 날 C# 9.0으로 빌드했는데 프로그램 동작이 바뀐다면 얼마나 황당하겠습니까? ^^;)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 언어 스펙에 따른 원인을 밝혔으니, 마지막으로 IL 코드 상의 차이점을 파악해 볼까요? ^^ (사실 IL 코드까지 출력된 상황에서는 이미 언어 스펙 상의 타입 결정이 완료된 상태이므로 큰 의미는 없습니다.)<br /> <br /> 첫 번째의 경우처럼 1과 2를 직접 전달하거나,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > /* 0x000002B6 17 */ IL_005A: <span style='color: blue; font-weight: bold'>ldc.i4.1</span> /* 0x000002B7 2802000006 */ IL_005B: call void ConsoleApp2.Program::M(<span style='color: blue; font-weight: bold'>int16</span>) /* 0x000002BC 00 */ IL_0060: nop /* 0x000002BD 18 */ IL_0061: <span style='color: blue; font-weight: bold'>ldc.i4.2</span> /* 0x000002BE 2802000006 */ IL_0062: call void ConsoleApp2.Program::M(<span style='color: blue; font-weight: bold'>int16</span>) </pre> <br /> -128 ~ 127 사이의 숫자를 전달하는 경우는 M(int16) 메서드를 선택해 IL 코드를 산출해 내고 있는 반면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > M(49); /* 0x000002C4 1F31 */ IL_0068: <span style='color: blue; font-weight: bold'>ldc.i4.s 49</span> /* 0x000002C6 2802000006 */ IL_006A: call void Program::M(<span style='color: blue; font-weight: bold'>int16</span>) </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;' > /* 0x000002C6 02 */ IL_006A: ldarg.0 /* 0x000002C7 8E */ IL_006B: ldlen /* 0x000002C8 2C03 */ IL_006C: brfalse.s IL_0071 /* 0x000002CA 18 */ IL_006E: <span style='color: blue; font-weight: bold'>ldc.i4.2</span> /* 0x000002CB 2B01 */ IL_006F: br.s IL_0072 /* 0x000002CD 17 */ IL_0071: <span style='color: blue; font-weight: bold'>ldc.i4.1</span> /* 0x000002CE 6A */ IL_0072: <span style='color: blue; font-weight: bold'>conv.i8</span> /* 0x000002CF 2803000006 */ IL_0073: call void ConsoleApp2.Program::M(<span style='color: blue; font-weight: bold'>int64</span>) </pre> <br /> 마찬가지로 (ldc.i4.1, ldc.i4.2) 상수 명령어가 오지만 마지막에 conv.i8을 이용해 M(int64)를 부르기 위한 변환을 합니다. 뭐 이 정도!<br /> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1649&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
2126
(왼쪽의 숫자를 입력해야 합니다.)