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# / January 2008

Tip: Looking for answers? Try searching our database.

specialized generic method in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
My interest - 02 Jan 2008 20:25 GMT
Can I create a specialized generic method depends on the constraint on
the generic type constraint?  such as:

static class foo
{
   public static void f<T_>(T_ dummy) where T_: struct    { ....}
   public static void f<T_>(T_ dummy) where T_: class    { ....}
}

Thanks.
Ignacio Machin ( .NET/ C# MVP ) - 02 Jan 2008 20:47 GMT
Hi,

Did you try to compile it?

It gives an error "Type 'foo' already defines a member called 'f' with the
same parameter types" which says basically that you need to have a
difference in parameters set between the methods.

> Can I create a specialized generic method depends on the constraint on
> the generic type constraint?  such as:
[quoted text clipped - 6 lines]
>
> Thanks.

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

My interest - 03 Jan 2008 17:54 GMT
Yes, I know the compiler will generate this error message.  What I try
to ask is how can I get around with this if I want different f() being
called for different type of parameter (e.g. overloading)  .. by
putting a where constraint.

For example, in a non-generic world, this is valid:

static class foo
{
   public static void f(double dummy)  { ....}
   public static void f(Array dummy) { ....}
}

Can I do sth similar in the generic world?  I guess one way is just to
create one f() and do dispatch inside the code by checking
tyeof(dummy), but it will be very messy / inconvenient.  So I am
trying to seek an alternatively approach.
Jon Skeet [C# MVP] - 03 Jan 2008 18:12 GMT
> Yes, I know the compiler will generate this error message.  What I try
> to ask is how can I get around with this if I want different f() being
[quoted text clipped - 13 lines]
> tyeof(dummy), but it will be very messy / inconvenient.  So I am
> trying to seek an alternatively approach.

No, you can't overload by type constraints (which is effectively what
you're trying to do).

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


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.