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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:18:01+00:00 2026-06-12T04:18:01+00:00

I am implementing a custom collection implementation that can either be readonly or non-readonly;

  • 0

I am implementing a custom collection implementation that can either be readonly or non-readonly; that is, all the methods that change the collection call a function that is the moral equivalent of:

private void ThrowIfReadOnly() {
    if (this.isReadOnly)
       throw new SomeException("Cannot modify a readonly collection.");
}

I am not sure which of NotSupportedException or InvalidOperationException I should use in that case.

  • 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-12T04:18:03+00:00Added an answer on June 12, 2026 at 4:18 am

    The MSDN only has one bit of guidance on this precise topic, on NotSupportedException:

    For scenarios where it is sometimes possible for the object to perform the requested operation, and the object state determines whether the operation can be performed, see InvalidOperationException.

    What follows is purely my own interpretation of the rule:

    • If the object’s state can change so that the operation can become invalid / valid during the object’s lifetime, then InvalidOperationException should be used.
    • If the operation is always invalid / valid during the whole object’s lifetime, then NotSupportedException should be used.
    • In that case, “lifetime” means “the whole time that anyone can get a reference to the object” – that is, even after a Dispose() call that often makes most other instance methods unusable;
      • As pointed out by Martin Liversage, in the case of an object having been disposed, the more specific ObjectDisposedException type should be used. (That is still a subtype of InvalidOperationException).

    The practical application of these rules in that case would be as follows:

    • If isReadOnly can only be set at the time when the object is created (e.g. a constructor argument), and never at any other time, then NotSupportedException should be used.
    • If isReadOnly can change during the lifetime of the object, then InvalidOperationException should be used.
      • However, the point of InvalidOperationException vs NotSupportedException is actually moot in the case of implementing a collection – given the description of IsReadOnly on MSDN, the only permitted behavior for IsReadOnly is that its value never changes after the collection is initialized. Meaning that a collection instance can either be modifiable or read-only – but it should choose one at initialization and stick with it for the rest of its lifetime.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone suggest a good implementation of a generic collection class that implements the
Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only
I'm implementing a custom control that inherits from Control . I want it to
I am implementing custom deploy Task in SBT and I need to copy all
Suppose I have a custom collection class that provides some internal thread synchronization. For
Why do we need custom collection and custom enumeration by implementing IEnumerable and IEnumerator
I have a Page class implementing java.util.Collection as well as other methods like hasPreviousPage
Implementing custom DataAnnotationsModelMetadataProvider in ASP.NET MVC2. Assuming the object that is being rendered looks
I have a custom class that implements ICollection , and this class is readonly,
I'm having problems implementing IEnumerable<T> in my custom collection class in C++/CLI. Here is

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.