Will C# support primitive data type in Generics
-
Public class MyClass where T:int { } why it's giving error. why .net framework not supporting
-
why this restriction. Allowing struct. what problem allowing int?
You'd have to ask Anders Hejlsberg[^]. It's the way generics were built into C#. Search for generic type constraints and you might find some reasoning. MSDN[^]
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Public class MyClass where T:int { } why it's giving error. why .net framework not supporting
What types do you expect you should be able to use with "int" as a constraint? You would only be able to use a single type - "int" itself (no uint, long etc. - those don't derive from int). But if there's only one possible type argument, it doesn't make sense to use generics at all!
-
What types do you expect you should be able to use with "int" as a constraint? You would only be able to use a single type - "int" itself (no uint, long etc. - those don't derive from int). But if there's only one possible type argument, it doesn't make sense to use generics at all!
Hi, you might want to enumerate the acceptable types and use common features:
public class MyClass<T> where T : int, long, float, double
and now perform addition. :)Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, you might want to enumerate the acceptable types and use common features:
public class MyClass<T> where T : int, long, float, double
and now perform addition. :)Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
But multiple constraints mean the type
T
must implement all of those. There is no way to say "int OR float" (which would have to automatically generate an interface with all methods common between int and float). But unfortunately, .NET doesn't support 'dynamic interfaces' (you declare an interface, and all classes having the appropriate methods will automatically implement it). Actually, I think VB10 will have something dynamic interfaces, but I guess it'll be implemented on top of Reflection/the DLR. -
But multiple constraints mean the type
T
must implement all of those. There is no way to say "int OR float" (which would have to automatically generate an interface with all methods common between int and float). But unfortunately, .NET doesn't support 'dynamic interfaces' (you declare an interface, and all classes having the appropriate methods will automatically implement it). Actually, I think VB10 will have something dynamic interfaces, but I guess it'll be implemented on top of Reflection/the DLR.Thanks. Syntax proposal:
public class MyClass<T> where T in { int, long, float, double }
:)Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Thanks. Syntax proposal:
public class MyClass<T> where T in { int, long, float, double }
:)Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Right, that would be good. Or the numeric types should implement some sort of IDoMath interface and we could use that. I also want an enum constriant.
-
Right, that would be good. Or the numeric types should implement some sort of IDoMath interface and we could use that. I also want an enum constriant.
If they made it so we had a Numeric constraint, or they implemented some common interface (INumeric), that would fix it for all the integral data types and enums too in one go.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
If they made it so we had a Numeric constraint, or they implemented some common interface (INumeric), that would fix it for all the integral data types and enums too in one go.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)I want enums separate from the other types. I have generic classes that should accept enums only. As it stands, I have to check at runtime and throw and Exception. X|
-
public class MyClass<T>
{
...
}MyClass<int> myClass = new MyClass<int>();
is this what you need ?
public class CplusEnumExtension : whehre T:int { public CplusEnumExtension(int t) { enumT = t; } public bool Has(F findvalue) { return ((int)(object)enumT & (int)(object)findvalue)>0; } private int enumT=0; } In above code Has method i comparing int type that's why i expecting int from client side. Apartment from this I have implemented code below in C++ which client can use any type.(int, long etc) Is it possible in C# //EnumT->int,long,Ulong...0x0001; //Findvalue->ePDVE_DllError //usage : CEnumerationExtensions<ULONGLONG,ePileplandesign_verificationError> obj(errorcode); template ; class CEnumerationExtensions { public: CEnumerationExtensions(EnumT e) : enum_(e) {} public: bool Has(Findvalue value) { return ((enum_ & static_cast(value))>0); } private: EnumT enum_; };