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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:49:49+00:00 2026-05-24T22:49:49+00:00

I’ve recently decided to refresh my memory regarding C# basics, so this might be

  • 0

I’ve recently decided to refresh my memory regarding C# basics, so this might be trivial, but i’ve bumped into the following issue:

StringCollection was used in .NET v1.0 in order to create a strongly typed collection for strings as opposed to an object based ArrayList (this was later enhanced by including Generic collections):

Taking a quick glance at StringCollection definition, you can see the following:

// Summary:
//     Represents a collection of strings.
[Serializable]
public class StringCollection : IList, ICollection, IEnumerable
{
...
    public int Add(string value);
...
}

You can see it implements IList, which contains the following declaration (among a few other declarations):

int Add(object value);

But not:

int Add(string value);

My first assumption was that it is possible due to the .NET framework covariance rules.

So just to make sure, I tried writing my own class which implements IList and changed

int Add(object value);

to retrieve a string type instead of an object type, but for my surprise, when trying to compile the project, I got an compile-time error:

does not implement interface member 'System.Collections.IList.Add(object)'

Any ideas what causes this?

Thanks!

  • 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-24T22:49:50+00:00Added an answer on May 24, 2026 at 10:49 pm

    The behavior is caused by the explicit implementation of IList.Add(object) rather than co/contravariance. Per the MSDN documentation, StringCollection explicitly implements IList.Add(object); the Add(string) method is unrelated. The implementation may resemble something like this:

    class StringCollection : IList
    {
        ...
        public int Add(string value)
        {} // implementation
    
        public int IList.Add (object value)
        {
            if (!value is string)) return -1;
            return Add(value as string)
        }
    }
    

    This distinction can be observed:

      StringCollection collection = new StringCollection();
      collection.Add(1); // compile error
      (collection as IList).Add(1); // compiles, runtime error
      (collection as IList).Add((object)"") // calls interface method, which adds string to collection
    

    Addendum

    The above doesn’t address why this pattern is implemented. The C# language specification states that [§13.4.1, emphasis added]:

    In some cases, the name of an interface member may not be appropriate
    for the implementing class, in which case the interface member may be
    implemented using explicit interface member implementation. […]

    It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.

    StringCollection adheres to the required IList behavior — IList makes no guarantee that any arbitrary object can be added to it. StringCollection makes stronger guarantees — primarily, that it will contain only strings. The class includes its own strongly-typed methods for Add, Contains, Item, and others for the standard use case where it is accessed as a StringCollection rather than an IList. But it still functions perfectly well as an IList, accepting and returning objects, but returning an error code (as IList permits) if an attempt is made to add an item that is not a string.

    Ultimately, whether an interface shows up in the class (i.e., is explicitly implemented) is at the discretion of the class author. In the case of framework classes, explicit implemenentations are included in the MSDN documentation but are not accessible as class members (e.g., shown in autocompletion contexts).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.