Instantiate an already typed generic.
-
2012년 3월 6일 화요일 오전 12:09
Is there a shorter way of instantiating a generic type in the scenario below?
class Foo { private Bar<int,string,int,string> _bar; public Foo() { _bar = new Bar<int,string,int,string>(); // is there no shortcut ?? // e.g. // _bar = new Bar<,,,>(); } }
모든 응답
-
2012년 3월 6일 화요일 오전 1:00
No there is no shortcut. After you type the ... = new Bar <, doesn't the intellisense "suggest" the rest of the line?
--
Mike- 답변으로 제안됨 Prasanna A 2012년 3월 6일 화요일 오전 6:26
- 답변으로 표시됨 OldPhantom 2012년 3월 7일 수요일 오전 4:37
-
2012년 3월 6일 화요일 오전 1:53Thanks for the answer. I was just hoping that the actual code duplication could have been avoided.
-
2012년 3월 6일 화요일 오전 6:05
public class Bar<T,T> { }Bar<int, string> bar = new Bar<int, string>();
There isn't a shorter way, I second Mike's answer.- 답변으로 제안됨 Prasanna A 2012년 3월 6일 화요일 오전 6:26
-
2012년 3월 6일 화요일 오전 6:33
There is much sorter way.. have an alias for Bar<t1,t2,t3,t4> and use it as below.
using Bar = <NameSpaceName>.Bar<int,string,int,string>; //Must be fully qualified name - <NameSpace>.<class> class Foo { private Bar _bar; public Foo() { _bar = new Bar(); } }
I hope this helps.Please mark this post as answer if it solved your problem. Happy Programming!
- 답변으로 표시됨 OldPhantom 2012년 3월 7일 수요일 오전 4:37
-
2012년 3월 6일 화요일 오전 8:14
Adavesh,
Syntactically yes, the alias is simpler to use and read as mention by you.
I understand to the compiler it doesn't differ much as we need to pass the type for instantiating without which its won't be possible.Thanks,
PA -
2012년 3월 6일 화요일 오전 10:15
Adavesh,
Syntactically yes, the alias is simpler to use and read as mention by you.
I understand to the compiler it doesn't differ much as we need to pass the type for instantiating without which its won't be possible.Thanks,
PAI don't get you. Can you exaplain ?
Please mark this post as answer if it solved your problem. Happy Programming!
-
2012년 3월 6일 화요일 오후 11:26

