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 215863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:30:38+00:00 2026-05-11T18:30:38+00:00

I ran across a compilation issue today that baffled me. Consider these two container

  • 0

I ran across a compilation issue today that baffled me. Consider these two container classes.

public class BaseContainer<T> : IEnumerable<T>
{
    public void DoStuff(T item) { throw new NotImplementedException(); }

    public IEnumerator<T> GetEnumerator() { }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { }
}
public class Container<T> : BaseContainer<T>
{
    public void DoStuff(IEnumerable<T> collection) { }

    public void DoStuff <Tother>(IEnumerable<Tother> collection)
        where Tother: T
    {
    }
}

The former defines DoStuff(T item) and the latter overloads it with DoStuff <Tother>(IEnumerable<Tother>) specifically to get around the absence of covariance/contravariance of C# (until 4 I hear).

This code

Container<string> c = new Container<string>();
c.DoStuff("Hello World");

hits a rather strange compilation error. Note the absence of <char> from the method call.

The type ‘char’ cannot be used as type parameter ‘Tother’ in the generic type or method ‘Container.DoStuff(System.Collections.Generic.IEnumerable)’. There is no boxing conversion from ‘char’ to ‘string’.

Essentially, the compiler is trying to jam my call to DoStuff(string) into Container.DoStuff<char>(IEnumerable<char>) because string implements IEnumerable<char>, rather than use BaseContainer.DoStuff(string).

The only way I’ve found to make this compile is to add DoStuff(T) to the derived class

public class Container<T> : BaseContainer<T>
{
    public new void DoStuff(T item) { base.DoStuff(item); }

    public void DoStuff(IEnumerable<T> collection) { }

    public void DoStuff <Tother>(IEnumerable<Tother> collection)
        where Tother: T
    {
    }
}

Why is the compiler trying to jam a string as IEnumerable<char> when 1) it knows it can’t (given the presence of a compilation error) and 2) it has a method in the base class that compiles fine? Am I misunderstanding something about generics or virtual method stuff in C#? Is there another fix other than adding a new DoStuff(T item) to Container?

  • 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-05-11T18:30:38+00:00Added an answer on May 11, 2026 at 6:30 pm

    Edit

    Ok… I think I see your confusion now. You would have expected DoStuff(string) to have kept the parameter as a string and walked the BaseClass Method List first looking for a suitable signature, and failing that fallback to trying to cast the parameter to some other type.

    But it happened the other way around… Instead Container.DoStuff(string) went, meh “theres a base class method there that fits the bill, but I’m going to convert to an IEnumerable and have a heart attack about what’s available in the current class instead…

    Hmmm… I’m sure Jon or Marc would be able to chime in at this point with the specific C# Spec paragraph covering this particular corner case

    Original

    Both Methods expect an IEnumerable Collection

    You’re passing an individual string.

    The compiler is taking that string and going,

    Ok, I have a string, Both methods
    expect an IEnumerable<T>, So I’ll
    turn this string into an
    IEnumerable<char>… Done

    Right, Check the first method…
    hmmm… this class is a
    Container<string> but I have an
    IEnumerable<char> so that’s not right.

    Check the second method, hmmm…. I
    have an IEnumerable<char> but char
    doesn’t implement string so that’s
    not right either.

    COMPILER ERROR

    So what#s the fix, well it completely depends what your trying to achieve… both of the following would be valid, essentially, your types usage is just incorrect in your incarnation.

            Container<char> c1 = new Container<char>();
            c1.DoStuff("Hello World");
    
            Container<string> c2 = new Container<string>();
            c2.DoStuff(new List<string>() { "Hello", "World" });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran across an interesting issue today. We have an application that utilizes Zend
I ran across a class that was set up like this: public class MyClass
I ran across some code like this today; having a flow control flag that
I recently ran across a class that had the following field declared: private final
I ran across a tutorial that had the following syntax: rails generate model User
I ran across an interesting issue in some of my humanize_bytes() code. This loop
I ran across the following class in a C# XNA graphics api and I
I'm trying to fix a design flaw that I recently ran across in some
I ran across an issue whenever I was trying to sort a vector of
I just ran across a problem that took a little while to figure out

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.