471,318 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Can a method be declared with a parameterized return type?

It doesn't look like something like this is possible in 2.0:
public <T> doStuff();

Am I correct, and if so, there's probably a good reason that I can't
think of why not...
Dec 1 '05 #1
5 11673
You can write a generic method whose return type is its generic type
parameter, but just like all generic definitions, the angle brackets
and type parameters come after the name:

public T DoStuff<T>()
{
...
}

Is that what you're looking for?

Jesse

Dec 1 '05 #2
That is what I'm looking for, but attempting a trivial sample is giving
unexpected results.

I would expect the following console app to print "bobo". Instead I get
some crazy type info:

public delegate T AnonymousWithReturn<T>();

static void Main( string[] args )
{
AnonymousWithReturn<string> test = delegate() { return "bobo"; };
Console.WriteLine( test );
Console.ReadLine();
}
jm*****@gmail.com wrote:
You can write a generic method whose return type is its generic type
parameter, but just like all generic definitions, the angle brackets
and type parameters come after the name:

public T DoStuff<T>()
{
...
}

Is that what you're looking for?

Dec 1 '05 #3
In your example you haven't executed the delegate; just replace with

Console.WriteLine(test());

and you will be sorted

Marc

"Brad Wood" <bradley|.wood|@ndsu|.edu> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
That is what I'm looking for, but attempting a trivial sample is giving
unexpected results.

I would expect the following console app to print "bobo". Instead I get
some crazy type info:

public delegate T AnonymousWithReturn<T>();

static void Main( string[] args )
{
AnonymousWithReturn<string> test = delegate() { return "bobo"; };
Console.WriteLine( test );
Console.ReadLine();
}
jm*****@gmail.com wrote:
You can write a generic method whose return type is its generic type
parameter, but just like all generic definitions, the angle brackets
and type parameters come after the name:

public T DoStuff<T>()
{
...
}

Is that what you're looking for?

Dec 1 '05 #4
Thanks (he said sheepishly)..

Marc Gravell wrote:
In your example you haven't executed the delegate; just replace with

Console.WriteLine(test());

Dec 1 '05 #5
Brad,
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".

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
"Brad Wood" <bradley|.wood|@ndsu|.edu> wrote in message
news:eY**************@tk2msftngp13.phx.gbl...
| It doesn't look like something like this is possible in 2.0:
| public <T> doStuff();
|
| Am I correct, and if so, there's probably a good reason that I can't
| think of why not...
Dec 2 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Antoine Pitrou | last post: by
10 posts views Thread by Mark | last post: by
4 posts views Thread by Alan T | 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.