473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 11731
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Antoine Pitrou | last post by:
Hi, I've been looking at writing parameterized metaclasses and here are the two solutions I've come to: (my goal was to build a way to automatically add a hash function that would take into...
1
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
10
by: Mark | last post by:
I have an abstract class, and a set of classes that inherit from my abstract class. The fact that it is abstract is likely irrelevant. I have a static factory method in my abstract class that...
2
by: Stig Nielsson | last post by:
I am wondering what is the most efficient way to make a parameterized factory method, and before I start spending time on performance measurements myself, I would like to hear the NG's opinion: ...
7
by: Rene | last post by:
We all know that we can't call custom methods or properties form generic type parameters (<T>) by default because the compiler will complain about his. For example, the following won't normally...
12
by: acb | last post by:
Hi, I have a list of different objects in a <List> Structure. There is only one category of each kind of object. Current I have the following methods: public static Flag GetFlagObj() {...
4
by: Alan T | last post by:
I got a method in my ancestor form declared as Protected, this method has empty body. In my descendant form I declared as Protected also, then compile has no problem but the name of the method has...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.