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 / .NET Framework / .NET SDK / March 2006

Tip: Looking for answers? Try searching our database.

C# generics: wildcards

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Puce - 07 Mar 2006 16:56 GMT
Hi,

I've got the following classes and interfaces:

public interface ICommand<T> where T : IResult
{
T Process();
}

----

interface IResult...

----

class AuthenticationResult : IResult...

----

class AuthenticationCommand : ICommand<AuthenticationResult>{

AuthenticationResult Process(){...}
}

-----

With Java I could now do something like the following somewhere in my program:

ICommand<?> command = new AuthenticationCommand();
IResult result = command.Process();

But C# does not know wildcards. Is there a work-around without using casts?

Thanks!

-Puce
Helge Jensen - 07 Mar 2006 18:42 GMT
> With Java I could now do something like the following somewhere in my program:
>
> ICommand<?> command = new AuthenticationCommand();
> IResult result = command.Process();
>
> But C# does not know wildcards. Is there a work-around without using casts?

In C#3:

var command = new AuthenticationCommand();
IResult result = command.Process();

Which would assign the type "AuthenticationCommand" to command -- not
exactly wildcarding but syntactically similar in this case. In C#2 you
have lost with your exact code.

You can use the explicit type in the declaration of command:

 ICommand<AuthenticationResult> command = new AuthenticationCommand();

You might consider making ICommand unparametrised:

 interface ICommand { IResult Process(); }

if you really need to uniformly treat results.

An alternative approach, which I don't really like too much is:

   interface IResult { }
   interface ICommand { IResult Process(); }
   interface ICommand<T>: ICommand where T: IResult { new T Process(); }
   class Foo: IResult {}
   class X : ICommand<Foo> {
       IResult ICommand.Process() { return Process(); }
       public Foo Process() { return new Foo(); }
   }
   class Program
   {
       static void Main(string[] args)
       {
           ICommand command = new X();
           IResult r = command.Process();
       }
   }

Signature

Helge Jensen
 mailto:helge.jensen@slog.dk
 sip:helge.jensen@slog.dk
              -=> Sebastian cover-music: http://ungdomshus.nu <=-


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.