tags:

views:

65

answers:

3

Given the example below, can someone please show me how this could be called?

bool WithinDelta<T>(T input1, T input2, T delta)

Ive tried various ways such as

bool foo = GenericMath.WithinDelta(1, 50, 75);
bool foo = GenericMath.WithinDelta<int>(1, 50, 75);

but the Type <T> is throwing me off.

Sorry for the basic question, but Im sick of beating my head on the desk over something this basic.

+1  A: 

both ways are correct

BlackTigerX
+2  A: 

Both of those should work fine. What's going wrong?

The first way is using type inference which works out the most appropriate type of T based on the arguments. It only works with generic methods rather than generic types, and there are various restrictions - although it's much more powerful in C# 3 than in C# 2.

Jon Skeet
just crashes with Could not load file or assembly 'MiscUtil, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d3c42c4bfacf7596' or one of its dependencies. The system cannot find the file specified.
Leroy Jenkins
So the call is correct and correctly compiled but the code fail to run in fact.You should ensure that the correct assembly could be found by your executable.
VirtualBlackFox
That's nothing to do with the generics - you've got an assembly version mismatch or invalid assembly reference in your project
thecoop
thats what I thought, but when I use GenericMath.Equals it works just fine.
Leroy Jenkins
I'm looking at the code for MiscUtil now to see what I can find - assuming this is my library...
Jon Skeet
We don't have a GenericMath.Equals method... which means you'd have been using Object.Equals instead, accidentally... which means that MiscUtil wouldn't have tried to load. Sounds like a simple case of not making your dependencies available properly.
Jon Skeet
Well if I was using Object.Equals then yeah I guess it would make sense.. now I just need to go find out how to correct it.Thanks again!
Leroy Jenkins
+1  A: 

As other people have pointed out both ways work and in this case are equivalent.

To give you another example which may help with the confusion, or potentially make it worse, you can also do the following

bool foo = GenericMath.WithinDelta<double>(1, 50, 75);

In this case the generic argument will force the type parameters of WithinDelta to be double values. So the compiler will then go through the process of ensuring the integer literals are converted to doubles before calling.

JaredPar