473,718 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11743
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 AnonymousWithRe turn<T>();

static void Main( string[] args )
{
AnonymousWithRe turn<string> test = delegate() { return "bobo"; };
Console.WriteLi ne( test );
Console.ReadLin e();
}
jm*****@gmail.c om 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.WriteLi ne(test());

and you will be sorted

Marc

"Brad Wood" <bradley|.wood| @ndsu|.edu> wrote in message
news:%2******** ********@TK2MSF TNGP12.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 AnonymousWithRe turn<T>();

static void Main( string[] args )
{
AnonymousWithRe turn<string> test = delegate() { return "bobo"; };
Console.WriteLi ne( test );
Console.ReadLin e();
}
jm*****@gmail.c om 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.WriteLi ne(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<obj ect>();
MemoryStream m = doSomething<Mem oryStream>();

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 GetCustomAttrib ute (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******** ******@tk2msftn gp13.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
2274
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 account a selected list of object attributes) 1. all-in-one metametaclass: class Autohash2(type):
1
6076
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
14430
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 the class and this works but I would also like to know of other ways to do this. Also are there any performance implacations of using sealed?
10
5138
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 creates subclasses. The constructor in my subclasses must be able to call the constructor in my base class. For the factory method in my abstract class to call the constructor on my subclasses, the constructor in the subclass MUST be public (or...
2
1712
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: I want my parameterized factory method to create either an object of type A or an object of type B, depending on the type of the Input parameter in the factory method. The type can be tested using the 'is' operator, but alternatively, the input...
7
1334
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 compile: T.MyCustomMethod() The compiler will tell me that it's not sure that the generic type parameters (<T>) will contain such method and will requires me to constrain the generic type parameters.
12
2262
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() { foreach (Thingy s in _Thingies)
4
4785
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 green underline. The warning of the compilation is about the method hides inherited member. Use the new keyword if hiding was intened.
8
8052
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: WHERE LIKE '" & ASPvar & "' % ORDER BY ... However, my call is similar to: conn.qMyLookup strVar, rs
0
8827
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8724
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9358
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9121
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9053
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7991
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5971
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2559
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.