Why did you invent "Generics Functions" like this?

  • Thread starter Thread starter Robert Heuvel
  • Start date Start date
R

Robert Heuvel

Why did you devise invoking generics functions like you did?
I understand that the method of use is "compatible" to that of generic classes
but isn't it a bit clumsy? Or am I missing something?

Wouldn't it have been "cooler" to have just been able to invoke a generic method
without having to state which type you want to use it with (in questionable
cases one could always revert to type casting)? The paramaters would be
suffictient to identify the type wouldn't they?

Generic Funtion:
public ItemType GenericFunction<ItemType>(ItemType
item)
{
// (Process item here.)
return item;
}
The code that calls GenericFunction() defines the type, which is then used for a
parameter to the function and a method.

way to invoke at the time:
string input = "...";
string output = Obj.GenericFunction<string>(input);

how about this?
string input = "...";
// input is string, so thats the type to use...
string output = Obj.GenericFunction(input);
 
Robert Heuvel said:
Why did you devise invoking generics functions like you did?
I understand that the method of use is "compatible" to that of generic
classes but isn't it a bit clumsy? Or am I missing something?

Wouldn't it have been "cooler" to have just been able to invoke a
generic method without having to state which type you want to use it
with (in questionable cases one could always revert to type casting)?
The paramaters would be suffictient to identify the type wouldn't
they?
<snip>

The C# 2.0 language specification, available here:
http://msdn.microsoft.com/vcsharp/team/language/default.aspx

Section 19.1.5 states that:

"...In many cases, however, the compiler can deduce the correct type
argument from the other arguments passed to the method, using a process
called *type inferencing*..."

* added to denote emphasized text in original. This is exactly what
you're referring to.

Example:

public T Foo<T>(T value)
{
...
}

String s = Foo("Hello world");
Int32 i = Foo(10);

this, according to the spec, should work as you expect it to, where the
first call to Foo would call Foo<String> and the second call would use
Foo<Int32>.
 
Back
Top