 |
 | I like more this one Alberto Bencivenni | 1:31 6 Feb '08 |
|
 |
public static void LimitRange<T>(T low, ref T value, T high) where T : IComparable { if (value.CompareTo(low) < 0)
value = low;
else if (value.CompareTo(high) > 0)
value = high;
}
This way don't need to assign a new variable.
|
|
|
|
 |
 | Excellence milk for babes JadeZhao | 2:47 2 Feb '08 |
|
 |
Thanks for you help .Hope some deepness articles self-given for yourself or I will think you a layabout only copy the SDK handbook.
JadeZhao
|
|
|
|
 |
 | Great! Jean-Paul Mikkers | 10:34 1 Feb '08 |
|
 |
I like it. Microsoft should add this to their framework.
|
|
|
|
 |
 | Usage examples J4amieC | 3:24 1 Feb '08 |
|
 |
Am I missing something? Shouldn't:
int i = Clamp(12, 10, 0); -> i == 10
double d = Clamp(4.5, 10.0, 0.0); -> d == 4.5
be
int i = Clamp<int>(12, 10, 0); -> i == 10
double d = Clamp<double>(4.5, 10.0, 0.0); -> d == 4.5
|
|
|
|
 |
|
 |
You are missing something.
The examples use type inference, which means the compiler works out the type of the generic by the parameters passed in. Since the author is passing in fixed values, this is easy enough for the compiler to do. Not that what you posted is wrong of course... it's just more verbose than it needs to be.
Hope that helps.
|
|
|
|
 |
|
 |
thanks for clearing that up.
|
|
|
|
 |
 | Some comments. Ennis Ray Lynch, Jr. | 18:03 31 Jan '08 |
|
 |
T Result = Value; if (Value.CompareTo(Max) > 0) Result = Max; if (Value.CompareTo(Min) < 0) Result = Min; return Result;
Should be
T result = value; if (value.CompareTo(max) > 0) result = max; else if (value.CompareTo(min) < 0) result = min; return result;
Also from a best practices stand point your local variables as well as your parameters should be in camelCase and not PascalCase. It Is Much Easier To Read Applications When Casing Has Context, Plus It Is Easier To Type.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
 |
|
 |
Fair enough. Several sources agree with you, so I've changed the convention. I'm still adapting from K+R
|
|
|
|
 |
 | Usage? PCoffey | 4:00 31 Jan '08 |
|
 |
Can you give a couple of usage examples? it is interesting, but on the surface this looks like a generic method that was written for a very specific implementation.
|
|
|
|
 |
|
 |
You're right - especially given the generic syntax. I've posted two simple usage examples.
|
|
|
|
 |
|
 |
mikemccabe wrote: You're right - especially given the generic syntax.
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
|
|
|
|
 |
|
 |
cool. i get it now.
Thanks for the examples.
|
|
|
|
 |