Generic
-
C#:rose: How can add two numbers through generic function and the type is int or double or float anybody help me.:omg:
Continue...
-
C#:rose: How can add two numbers through generic function and the type is int or double or float anybody help me.:omg:
Continue...
I didn't test it but this should work:
T Add<T>(T x, T y) where T: int, double, float { return x+y; }
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
I didn't test it but this should work:
T Add<T>(T x, T y) where T: int, double, float { return x+y; }
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
You should have tested it then 'int' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. 'double' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. 'float' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. Operator '+' cannot be applied to operands of type 'T' and 'T'
only two letters away from being an asset
-
You should have tested it then 'int' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. 'double' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. 'float' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. Operator '+' cannot be applied to operands of type 'T' and 'T'
only two letters away from being an asset
Oh, thank you. That was too cool to work :-O Ok so unless I am missing something obvious it's not as easy as it looks...
IConvertible Add(T x, T y) where T : IConvertible { return (IConvertible)(x.ToDouble(CultureInfo.InvariantCulture)+y.ToDouble(CultureInfo.InvariantCulture)); }
Doesn't look that bad, but that's still not it. I don't want to return IConvertible but T, I want T to be only int, float or double (looks like this is impossible with
where
). Whatever, why not do justint Add(int x, int y){return x+y}; double Add(double x, double y){return x+y}; ...
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
I didn't test it but this should work:
T Add<T>(T x, T y) where T: int, double, float { return x+y; }
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
Thank u for helping me. I need ur help that code is not working properly. Give me the full source and ( where) what is the use of that one in generic. I expect get soon
Continue...
-
Thank u for helping me. I need ur help that code is not working properly. Give me the full source and ( where) what is the use of that one in generic. I expect get soon
Continue...
Unfortunately I was wrong as Mark pointed out.
where
doesn't allow specific structs as type constraint. See http://msdn2.microsoft.com/en-us/library/d5x73970(VS.80).aspx[^]. Becauseint
etc. are structures, you can't do this :( However, from what you said, it looks like easiest (and sufficient) thing to do is to have 3 overloads of Add(x,y) method - one for int, one for float, etc...
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe