Instantiate an already typed generic.
-
6 มีนาคม 2555 0: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<,,,>(); } }
ตอบทั้งหมด
-
6 มีนาคม 2555 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 6 มีนาคม 2555 6:26
- ทำเครื่องหมายเป็นคำตอบโดย OldPhantom 7 มีนาคม 2555 4:37
-
6 มีนาคม 2555 1:53Thanks for the answer. I was just hoping that the actual code duplication could have been avoided.
-
6 มีนาคม 2555 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 6 มีนาคม 2555 6:26
-
6 มีนาคม 2555 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 7 มีนาคม 2555 4:37
-
6 มีนาคม 2555 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 -
6 มีนาคม 2555 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!
-
6 มีนาคม 2555 23:26