How to specify a generic type as a restriction in a generic method?

You probably want to create an interface, and have your generic class implement that. MyGenericClass and MyGenericClass are considered to be completely different classes by the . NET compiler This would allow you to do something like this: void GetInstanceOf() where T : IMyGenericClass You could also use a shared base class: void GetInstanceOf() where T : MyGenericClassBase Finally, you might try to add a second type parameter to your method: void GetInstanceOf() where T: MyGenericClassBase.

You probably want to create an interface, and have your generic class implement that. MyGenericClass and MyGenericClass are considered to be completely different classes by the . NET compiler.

This would allow you to do something like this: void GetInstanceOf() where T : IMyGenericClass You could also use a shared base class: void GetInstanceOf() where T : MyGenericClassBase Finally, you might try to add a second type parameter to your method: void GetInstanceOf() where T: MyGenericClassBase (I'm not sure about that last one... I don't have a compiler handy to check it) See: msdn.microsoft.com/en-us/library/aa47985....

I finally chose to have the generic class to implement an interface and it worked fine. Thanks everybody! – Konamiman Oct 16 '09 at 7:16.

Interface IMyGenericBase { /* ... */ } class MyGenericClass : IMyGenericBase { /* ... */ } class MyDerivedGenericClass : MyGenericClass { /* ... */ } T GetInstanceOf() where T : IMyGenericBase { /* ... */ }.

I think you're making this much more complicated than it needs to be. Take a step back and solve the real problem, rather than trying to solve some completely unnecessary issue involving constraints. This seems very straightforward: MyGenericClass GetInstanceOfMyGenericClass() { return MyGenericClass.GetSingleton(); } You want a MyGenericClass, you say GetInstanceOfMyGenericClass(), done.

Why mess around with constraints at all if you don't need to?

If it were me I would likely create an interface, have 'MyGenericClass' implement that interface, and then use the interface in the 'where' of your method signature.

I agree with most posts here - providing an interface is typically the best approach. However, if MyGenericClass happens to be a class defined outside of your control, the other option is to do: void GetInstanceOf() where T : MyGenericClass Unfortunately, this requires your caller to provide the generic type of MyGenericClass as well, but it will compile and run correctly if you cannot change "MyGenericClass".

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions