Re: C# generic constraint
That's not valid C# code. You can't specify actual value types as generic constraints. Here is the relevant documentation for C#:
http://msdn.microsoft.com/en-us/library/bb384067.aspx
http://msdn.microsoft.com/en-us/library/d5x73970.aspx
The VB equivalent of:
csharp Code:
T SomeFunction<T>() where T : Type1, Type2
is:
vb.net Code:
Function SomeFunction(Of T As {Type1, Type2})() As T
Re: C# generic constraint
Actually, I think I may have missed the main point of your question. I think you were asking if it is possible to specify that the generic type parameter satisfies ANY of the specified constraints. The answer is no. If there are multiple constraints then the generic type parameter must satisfy ALL of them. To handle multiple types like that you would have to overload the method.
Re: C# generic constraint
Quote:
Originally Posted by
jmcilhinney
To handle multiple types like that you would have to overload the method.
Ok. And how would I go about that?
Re: C# generic constraint
Quote:
Originally Posted by
gavio
Ok. And how would I go about that?
You would open a search engine, type in overload method c# and then search. If you can't find what you need or can't understand what you find, then you'd post back here. There's gigabytes of information out there already. Don't ignore it. Look first, ask questions later.
Re: C# generic constraint
Quote:
Originally Posted by
jmcilhinney
You would open a search engine, type in overload method c# and then search. If you can't find what you need or can't understand what you find, then you'd post back here. There's gigabytes of information out there already. Don't ignore it. Look first, ask questions later.
I know what overloading a method looks like. But I can't see any value in it (the below example needs four more or less the same functions, which is quite ineffective):
Code:
int SomeFunction() { return 0; }
double SomeFunction() { return 0; }
string SomeFunction() { return String.Empty; }
Guid SomeFunction() { return Guid.NewGuid(); }
Unless I'm not using it right. That's why I asked, how would I go about it ;)
I have almost 5k posts - I know the rules and I don't ask before I do my search.
Re: C# generic constraint
You are, in that case, assuming that I know something that you don't on the subject. Overloading doesn't reduce the amount code. It simply means that have the same method name for the same task for different types. What I'm saying basically is that generics cannot help you here.
Re: C# generic constraint
Quote:
Originally Posted by
jmcilhinney
You are, in that case, assuming that I know something that you don't on the subject. Overloading doesn't reduce the amount code. It simply means that have the same method name for the same task for different types. What I'm saying basically is that generics cannot help you here.
In that case, thank you for all your time and patience :)