Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일

(시리즈 글이 4개 있습니다.)
닷넷: 2342. C# 14 - (1) (예약)
; https://www.sysnet.pe.kr/2/0/13970

닷넷: 2343. C# 14 - (2) 속성 구문에서 문맥 키워드로 추가되는 field 예약어
; https://www.sysnet.pe.kr/2/0/13971

닷넷: 2346. C# 14 - (3) Span 타입과 배열 간의 암시적 형변환
; https://www.sysnet.pe.kr/2/0/13974

닷넷: 2347. C# 14 - (4) 형식 인자가 없는 제네릭 타입의 nameof 지원
; https://www.sysnet.pe.kr/2/0/13975




C# 14 - (4) 형식 인자가 없는 제네릭 타입의 nameof 지원

이번 C# 신규 문법은 쉬어가는 코너가 되겠군요. ^^

Unbound generic types and nameof
; https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-14#unbound-generic-types-and-nameof

[Proposal]: Unbound generic types in nameof #8480
; https://github.com/dotnet/csharplang/issues/8480

말 그대로, 형식 인자를 제공하지 않은 제네릭 타입에 대해서도 nameof 구문을 적용할 수 있게 바뀌었습니다.

// 반드시 형식 인자를 제공했지만,
string text = nameof(List<string>);
Console.WriteLine(text); // 출력 결과: List

// C# 14부터 형식 인자를 생략하는 것도 가능
text = nameof(List<>);
Console.WriteLine(text); // 출력 결과: List

// 참고로 기존의 typeof도 형식 인자를 생략할 수 있었습니다.
var unboundType = typeof(List<>); // C# 13 이전에도 가능

개인적으로 처음 저걸 봤을 때는 쓸 상황이 얼마나 될까 싶었는데요, 문서를 읽어 보니 납득이 되는 부분이 좀 있습니다. ^^

Unbound generic types in nameof
; https://github.com/dotnet/csharplang/blob/main/proposals/unbound-generic-types-in-nameof.md

비단, 저 타입 자체의 이름을 가져와야 하는 경우로 한정 짓는다면 그다지 유용하지 않을 수도 있지만, 그 범위가 해당 제네릭 타입의 멤버까지도 넓어진다면 얘기가 살짝 달라집니다.

string text = nameof(Stack<>.Push); // 출력 결과: Push
Console.WriteLine(text);

public class Stack<T>
{
    public void Push(T item) { }
}

재미있는 건, 저 구문 자체가 nameof 내에서만 유효하다는 점입니다. 가령, 아래와 같은 코드는 컴파일 오류가 발생합니다.

// error CS0305: Using the generic type 'Stack<T>' requires 1 type arguments
var v = Stack<>.Push;

// typeof도 unbound 제네릭 타입의 멤버까지 지정하는 구문은 지원하지 않습니다.
var unboundMethod = typeof(Stack<>.Push); // error CS0426: The type name 'Push' does not exist in the type 'Stack<T>'




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 7/23/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  5  [6]  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13851정성태12/23/20245766디버깅 기술: 211. Windbg - 커널 모드 디버깅 상태에서 사용자 프로그램을 디버깅하는 방법
13850정성태12/23/20247204오류 유형: 940. "Application Information" 서비스를 중지한 경우, "This file does not have an app associated with it for performing this action."
13849정성태12/20/20247182디버깅 기술: 210. Windbg - 논리(가상) 주소를 Segmentation을 거쳐 선형 주소로 변경
13848정성태12/18/20246643디버깅 기술: 209. Windbg로 알아보는 Prototype PTE파일 다운로드2
13847정성태12/18/20246514오류 유형: 939. golang - 빌드 시 "unknown directive: toolchain" 오류 빌드 시 이런 오류가 발생한다면?
13846정성태12/17/20247327디버깅 기술: 208. Windbg로 알아보는 Trans/Soft PTE와 2가지 Page Fault 유형파일 다운로드1
13845정성태12/16/20245601디버깅 기술: 207. Windbg로 알아보는 PTE (_MMPTE)
13844정성태12/14/20247898디버깅 기술: 206. Windbg로 알아보는 PFN (_MMPFN)파일 다운로드1
13843정성태12/13/20245862오류 유형: 938. Docker container 내에서 빌드 시 error MSB3021: Unable to copy file "..." to "...". Access to the path '...' is denied.
13842정성태12/12/20246021디버깅 기술: 205. Windbg - KPCR, KPRCB
13841정성태12/11/20246949오류 유형: 937. error MSB4044: The "ValidateValidArchitecture" task was not given a value for the required parameter "RemoteTarget"
13840정성태12/11/20245923오류 유형: 936. msbuild - Your project file doesn't list 'win' as a "RuntimeIdentifier"
13839정성태12/11/20247327오류 유형: 936. msbuild - error CS1617: Invalid option '12.0' for /langversion. Use '/langversion:?' to list supported values.
13838정성태12/4/20246820오류 유형: 935. Windbg - Breakpoint 0's offset expression evaluation failed.
13837정성태12/3/20247686디버깅 기술: 204. Windbg - 윈도우 핸들 테이블 (3) - Windows 10 이상인 경우
13836정성태12/3/20245762디버깅 기술: 203. Windbg - x64 가상 주소를 물리 주소로 변환 (페이지 크기가 2MB인 경우)
13835정성태12/2/20247687오류 유형: 934. Azure - rm: cannot remove '...': Directory not empty
13834정성태11/29/20247759Windows: 275. C# - CUI 애플리케이션과 Console 윈도우 (Windows 10 미만의 Classic Console 모드인 경우) [1]파일 다운로드1
13833정성태11/29/20247062개발 환경 구성: 737. Azure Web App에서 Scale-out으로 늘어난 리눅스 인스턴스에 SSH 접속하는 방법
13832정성태11/27/20246607Windows: 274. Windows 7부터 도입한 conhost.exe
13831정성태11/27/20245546Linux: 111. eBPF - BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_MAP_TYPE_RINGBUF에 대한 다양한 용어들
13830정성태11/25/20247747개발 환경 구성: 736. 파이썬 웹 앱을 Azure App Service에 배포하기
13829정성태11/25/20247640스크립트: 67. 파이썬 - Windows 버전에서 함께 설치되는 py.exe
13828정성태11/25/20245965개발 환경 구성: 735. Azure - 압축 파일을 이용한 web app 배포 시 디렉터리 구분이 안 되는 문제파일 다운로드1
13827정성태11/25/20246977Windows: 273. Windows 환경의 파일 압축 방법 (tar, Compress-Archive)
13826정성태11/21/20247278닷넷: 2313. C# - (비밀번호 등의) Console로부터 입력받을 때 문자열 출력 숨기기(echo 끄기)파일 다운로드1
1  2  3  4  5  [6]  7  8  9  10  11  12  13  14  15  ...