Generics Question.

  • Thread starter Thread starter Ashish
  • Start date Start date
A

Ashish

This is just a thought, may be its not possible , but is it possible to
use generics in method declarations as return type arguments..

for example

public <T> Load(T value)
{
//method body
}

this would specify that the type passed would be type returned...

so at compile time, the compiler would do a static type checking to see
if there are compilation issues...


for exampple


Address addr = DataStore.Load(typeof(Address));
 
Hello!

Yes, you can use the type as the return type for the method and have the
compiler check for conversion errors.

You may also use the "where" keyword to access the interface of the type in
the method body.

public T Load(T value) where T : CustomType, ICustomInterface
{
//method body

return value;
}
 
Ashish,

Yes, it is, you would declare it like this:

public T Load<T>(T value)

However, you could not call it like you would. You would have to do
this to call it:

Address addr = DataStore.Load<Address>(addrInstance);

Hope this helps.
 
"Ashish" <[email protected]> a écrit dans le message de (e-mail address removed)...

| This is just a thought, may be its not possible , but is it possible to
| use generics in method declarations as return type arguments..
|
| for example
|
| public <T> Load(T value)
| {
| //method body
| }
|
| this would specify that the type passed would be type returned...

The above code will not compile as you are not specifying a return type. You
need to change it but...

| so at compile time, the compiler would do a static type checking to see
| if there are compilation issues...
|
| for exampple
|
| Address addr = DataStore.Load(typeof(Address));

IF this is what you want to achieve, then you need to do something more like
this :

public T Load<T>()
{
// method body
}

Used like this :

Address addr = DataStore.Load<Address>();

You do not need to pass any parameters to the function, just substitute the
generic parameter to strictly type the method.

Joanna
 
Ahsish... The general form is:

// generic method to invoke
public interface IInvoke<R,P>
{
R Invoke(P p);
}
public delegate R DInvoke<R,P>(P p);

// generic collection
public class JALGenericCollection<T,R,P> : Disposable, IInvoke<R,P>
where T : IDisposable, IInvoke<R,P>

Regards,
Jeff
 
Nicholas said:
Ashish,

Yes, it is, you would declare it like this:

public T Load<T>(T value)

However, you could not call it like you would. You would have to do
this to call it:

Address addr = DataStore.Load<Address>(addrInstance);

Hope this helps.

No, you certainly *can* call as if there was no template. The following
works just fine (not useful, but an example that just came to my mind):

class Program
{
static double ToDoubleInvar<T>(T val) where T : IConvertible
{
return
val.ToDouble(System.Globalization.CultureInfo.InvariantCulture);
}

static void Main(string[] args)
{
Console.WriteLine(ToDoubleInvar("3.0"));
Console.WriteLine(ToDoubleInvar(3f));
}
}

So you will be able to call it like:

Address addr = DataStore.Load(addrInstance);

Just my 2c
Stefan
 
Ashish,
As the other have shown you can define a method with a parameterized return
type.

Yes you can define a generic function where the parameter is only used for
the return type.

public T doSomething<T where T:new()>()

However! You need to supply the type parameter when you call the method
directly, something like:

object o = doSomething<object>();
MemoryStream m = doSomething<MemoryStream>();

However! it "violates" an FxCop rule as its "ambiguous". The compiler is not
able to use Type Inference to figure out the type parameter...

Here is a thread that discusses it:

http://groups.google.com/group/micr...bb32de857b1/cf4e46ca8f99b798#cf4e46ca8f99b798

Personally I find in the case of GetCustomAttribute (as the thread shows) it
makes sense as the type parameter is encapsulating the downcast, plus the
type parameter is used to "do work".

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| This is just a thought, may be its not possible , but is it possible to
| use generics in method declarations as return type arguments..
|
| for example
|
| public <T> Load(T value)
| {
| //method body
| }
|
| this would specify that the type passed would be type returned...
|
| so at compile time, the compiler would do a static type checking to see
| if there are compilation issues...
|
|
| for exampple
|
|
| Address addr = DataStore.Load(typeof(Address));
|
|
|
|
 
Back
Top