|
-
Jan 19th, 2012, 03:07 AM
#1
C# generic constraint
How to create a generic constraint, that only allows some predefined basic data types. Something like this:
Code:
T SomeFunction<T>() where T : int, double, string, Guid
{
// ...
return (T)someValue; // someValue can be of type: int, double, string or Guid
}
So, is this even possible?
-
Jan 19th, 2012, 04:51 AM
#2
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
-
Jan 19th, 2012, 04:55 AM
#3
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.
-
Jan 23rd, 2012, 03:44 AM
#4
Re: C# generic constraint
 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?
-
Jan 23rd, 2012, 05:11 AM
#5
Re: C# generic constraint
 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.
-
Jan 25th, 2012, 03:05 AM
#6
Re: C# generic constraint
 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.
-
Jan 25th, 2012, 03:33 AM
#7
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.
-
Jan 30th, 2012, 04:59 AM
#8
Re: C# generic constraint
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|