Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7798319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:58:18+00:00 2026-06-01T23:58:18+00:00

my problem is the following. I have made a code design for a home

  • 0

my problem is the following. I have made a code design for a home project which is apparently not working. Maybe you can help me to figure out where the “code smell” comes from.

Ok let’s start:
I have defined some classes to wrap around different kind of archive types:

public abstract class Archive { }
public class ZipArchive : Archive { }
public class TarArchive : Archive { }

To handle with those archives, I defined Manager classes.
An abstract one that defines the needed behaviour,

public abstract class ArchiveManager<T> where T : Archive
{ 
    public abstract void OpenArchive(T archive);
}

And the concrete ones, that actually implement the specific behaiour:

public class ZipArchiveManager : ArchiveManager<ZipArchive>
{
    public override void OpenArchive(ZipArchive archive) {  /* .. */ }
}

public class TarArchiveManager : ArchiveManager<TarArchive>
{
    public override void OpenArchive(TarArchive archive) {  /* .. */ }
}

What happens now is that during compile time, I dont know which kind of archives I will process, so I tried the following:

class Program
{
    static void Main(string[] args)
    {
        ArchiveManager<Archive> archiveManager = null;

        if (/*some condition*/) {            
            archiveManager = new ZipArchiveManager();
        }
        else {
            archiveManager = new TarArchiveManager();
        }
    }
}

which ended up in the following error:

Cannot implicitly convert type ‘ZipArchiveManager’ to
‘ArchiveManager’

As far as I understand, the generic argument cannot be implicitely converted. Is there any way to come around this? Does this code / design “smell”?

Thank you very much in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:58:20+00:00Added an answer on June 1, 2026 at 11:58 pm

    You can use a contravariant interface instead of an abstract class that doesn’t implement any functionality. In this case, you can only use the type parameter as a return value of a method, not as an argument:

    public interface IArchiveManager<out T>
        where T : Archive
    {
        T OpenArchive(Stream stream);
    }
    

    Then, simply implement the interface in your manager classes:

    public class ZipArchiveManager : IArchiveManager<ZipArchive>
    {
        public ZipArchive OpenArchive(Stream stream)
        {
            // ...
        }
    }
    
    public class TarArchiveManager : IArchiveManager<TarArchive>
    {
        public TarArchive OpenArchive(Stream stream)
        {
            // ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made the following code in a c# project: private void RandAddButton_Click(object sender,
I have the following problem: I made a UserControl in WPF which has an
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
I have the following problem using subversion: I'm currently working on the trunk of
I have made the following code to retrive data from SQLite database. public Cursor
I have the following problem. I'm writing my own class which must draw a
I have been working with a small project recently which involves Struts 2 and
I have made something like the following code: protected void Page_Load(object sender, EventArgs e)
I have the following problem / bug: I made a custom button-class (called CustomBlitButton)
I have following problem: when my session expires and user clicks on Ajax.Actionlink, Logon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.