using ddd = System.Console 처럼 사용하고 싶은데요
-
2012년 8월 14일 화요일 오전 1:57
using ddd = System.Console;
처럼List도 저렇게 사용하고 싶습니다.
아래처럼 사용하니 잘 안되네요.using List = System.Collections.Generic.List<T> ;
어떻게 사용하여야 하는지요?
모든 응답
-
2012년 8월 14일 화요일 오전 3:43
안녕하세요. 엄준일 입니다.
위의 질문처럼 특정 함수를 예약어처럼 사용하는 방법은 C# 언어에서 제공해 주지 않습니다.
Java, C++, C 언어에서도 언어적으로는 지원해 주지는 않지만, C++에서는 매크로 방식을 이용할 수 있습니다.그래서 예약어의 방식으로 처리하지 않고, 대리자(함수 포인터)를 이용하여 특정 메서드를 예약어처럼 사용할 수는 있습니다.
첫 번쨰,
아래의 코드는 Action 대리자를 이용하여 Console.WriteLine 함수가 위치하고 있는 곳을 참조하도록 하여, con 함수를 Console.WriteLine 함수처럼 이용할 수 있습니다.namespace ConsoleApplication1 { class Program { static Action<string> con = Console.WriteLine; static void Main(string[] args) { con("Hello World"); } } }두 번째,
List<T> 제네릭 컬렉션을 List 예약어로 사용하는 방법입니다.
첫 번째의 방법은 함수를 대상으로 하며, 두 번째인 이 방법은 클래스 형식을 대상으로 합니다.
List<T> 클래스가 이미 존재하는데 이것을 런타임에(실행시에) 클래스의 정의를 다른 이름으로 참조할 수 있는 방법은 없습니다. 이를 구현하려면 런타임에 동적으로 클래스(타입)을 정의하여 구현하는 C# 의 고급에 속하는 기술로 구현해야 합니다. 물론, 이것을 구현하려면 C# 과 .NET Framework 의 깊은 조예가 필요합니다.대신 이것을 상속을 이용하면 좀 더 간단하게 구현할 수 있습니다.
using System; namespace ConsoleApplication1 { public class GList<T> : System.Collections.Generic.List<T> { } public class GList : GList<Object> { } class Program { static void Main(string[] args) { GList<string> genericlist = new GList<string>(); genericlist.Add("Hello World"); // String 매개변수임 GList objectlist = new GList(); objectlist.Add("Hello World"); // Object 매개변수임 } } }
마지막으로,
위의 using 키워드는 네임스페이스를 예약어로 사용할 수 있는 기능입니다.예를 들면, 다음과 같이 구현할 수 있습니다.
using System; using G = System.Collections.Generic; using T = System.Text; namespace ConsoleApplication1 { class Program { public static void Main(string[] args) { // G 예약어 사용 G.List<string> list = new G.List<string>(); // T 예약어 사용 byte[] bdata = T.Encoding.Default.GetBytes("Hello World"); } } }
이해하시는데 작게나마 도움이 되셨으면 합니다.
감사합니다.
엄준일 (Junil, Um) Microsoft Visual Studio ALM MVP (Team System) Personal Blog : http://blog.powerumc.kr Visual Studio Korea Team Blog : http://vsts2010.net
- 답변으로 표시됨 잘몰라 2012년 8월 14일 화요일 오전 6:19
-
2012년 8월 14일 화요일 오전 3:48
using ddd = System.Console;
이라는 문장의 의미는 System.Console라는 네임스페이스의 별칭을 ddd로 사용하겠다는 의미입니다.
http://msdn.microsoft.com/ko-kr/library/sf0df423.aspx
단순히 줄여서 쓰는 용도이죠.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using console = System.Console; using Dinosaurs = System.Collections.Generic.List<string>; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //List<string> dinosaurs = new List<string>(); Dinosaurs dinosaurs = new Dinosaurs(); console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity); dinosaurs.Add("Tyrannosaurus"); dinosaurs.Add("Amargasaurus"); dinosaurs.Add("Mamenchisaurus"); dinosaurs.Add("Deinonychus"); dinosaurs.Add("Compsognathus"); console.WriteLine(); foreach (string dinosaur in dinosaurs) { console.WriteLine(dinosaur); } } } }System.Console.WriteLine은 객체생성없이 바로 불러서 사용이 가능하지만
System.Collections.Generic.List의 경우는 이런식으로 사용하셔야합니다.- 편집됨 GoldrushKoreaMVP 2012년 8월 14일 화요일 오전 4:38
- 답변으로 표시됨 잘몰라 2012년 8월 14일 화요일 오전 6:19
-
2012년 8월 14일 화요일 오전 6:20ㅎㅎ 반갑습니다.

