471,317 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Generics Question.

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));


Dec 19 '05 #1
8 1124
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;
}

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Dec 19 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ashish" <as*****@thisisjunk.com> wrote in message
news:Og**************@TK2MSFTNGP10.phx.gbl...
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));

Dec 19 '05 #3
Nicholas, please contact me privately by e-mail.

Thanks in advance.

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Dec 19 '05 #4
"Ashish" <as*****@thisisjunk.com> a écrit dans le message de news:
Og**************@TK2MSFTNGP10.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 19 '05 #5
thanks guys,
that was exactly what i was looking for !

Joanna Carter [TeamB] wrote:
"Ashish" <as*****@thisisjunk.com> a écrit dans le message de news:
Og**************@TK2MSFTNGP10.phx.gbl...

| 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

Dec 20 '05 #6
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

*** Sent via Developersdex http://www.developersdex.com ***
Dec 20 '05 #7
Nicholas Paldino [.NET/C# MVP] wrote:
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.Inva riantCulture);
}

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
Dec 20 '05 #8
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/micro...4e46ca8f99b798

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
"Ashish" <as*****@thisisjunk.com> wrote in message
news:Og**************@TK2MSFTNGP10.phx.gbl...
| 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));
|
|
|
|
Dec 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Bernardo Heynemann | last post: by
13 posts views Thread by Luc Vaillant | last post: by
1 post views Thread by Peter Kirk | last post: by
18 posts views Thread by riftimes | last post: by
11 posts views Thread by hammad.awan_nospam | last post: by
1 post views Thread by Kevin S. Goff | last post: by
7 posts views Thread by SpotNet | last post: by
13 posts views Thread by rkausch | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.