Jon,
| public T doSomething<T where T:new()>()
Yes you can define a generic function where the parameter is only used for
the return type.
However you need to supply the type parameter when you call the method,
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
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
| Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
| > Using the new Generics in C#, you can declare a Generic method, such as:
| >
| > public T doSomething<T>(T returnVal)
| > {
| > return returnVal;
| > }
|
| But could you do:
|
| public T doSomething<T where T:new()>()
| {
| return new T(); // Whatever the syntax is - can't remember offhand
| }
|
| Then:
|
| object o = doSomething();
| MemoryStream m = doSomething();
|
| You can in Java with scary amounts of type inference. Not sure I
| entirely approve, to be honest...
|
| --
| Jon Skeet - <sk***@pobox.com>
|
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too