Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

Generics Question.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ashish - 19 Dec 2005 22:09 GMT
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));
Anders Borum - 19 Dec 2005 22:19 GMT
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;
}

Signature

With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)

Nicholas Paldino [.NET/C# MVP] - 19 Dec 2005 22:22 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> This is just a thought, may be its not possible , but is it possible to
> use generics in method declarations as return type arguments..
[quoted text clipped - 14 lines]
>
> Address addr = DataStore.Load(typeof(Address));
Anders Borum - 19 Dec 2005 22:46 GMT
Nicholas, please contact me privately by e-mail.

Thanks in advance.

Signature

With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)

Stefan Simek - 20 Dec 2005 21:43 GMT
> Ashish,
>
[quoted text clipped - 8 lines]
>
>     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.InvariantCulture);
        }

        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
Joanna Carter [TeamB] - 19 Dec 2005 23:30 GMT
| This is just a thought, may be its not possible , but is it possible to
| use generics in method declarations as return type arguments..
[quoted text clipped - 7 lines]
|
| 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

Signature

Joanna Carter [TeamB]
Consultant Software Engineer

Ashish - 20 Dec 2005 00:34 GMT
thanks guys,
that was exactly what i was looking for !

> | This is just a thought, may be its not possible , but is it possible to
> | use generics in method declarations as return type arguments..
[quoted text clipped - 34 lines]
>
> Joanna
Jeff Louie - 20 Dec 2005 07:20 GMT
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
Jay B. Harlow [MVP - Outlook] - 23 Dec 2005 15:46 GMT
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/microsoft.public.dotnet.languages.csharp/browse_f
rm/thread/c96debb32de857b1/cf4e46ca8f99b798#cf4e46ca8f99b798


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".

Signature

Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net

| This is just a thought, may be its not possible , but is it possible to
| use generics in method declarations as return type arguments..
[quoted text clipped - 14 lines]
|
| Address addr = DataStore.Load(typeof(Address));

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.