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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:12:00+00:00 2026-05-14T20:12:00+00:00

Is there anyway I can do this? class TSOC<TCollection, TValue> : ICollection<TValue>, INotifyCollectionChanged where

  • 0

Is there anyway I can do this?

class TSOC<TCollection, TValue> : ICollection<TValue>, INotifyCollectionChanged where TCollection : ICollection
{
    private TCollection<TValue> collection;
}

It doesn’t like my definition of collection. I’d prefer the definition to look like TSOC<TCollection> where the user can pass in List<int> or something, but then I need to pull out the “int” to know what interface to implement.


Basically I just wanted to wrap all the ICollection methods in locks and force them to run on a particular thread, but still keep all the additional functionality of the programmer’s collection of choice…. I’m not sure this is possible though.

  • 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-14T20:12:00+00:00Added an answer on May 14, 2026 at 8:12 pm

    How about:

    class TSOC<TCollection, TValue> : ICollection<TValue>, INotifyCollectionChanged
        where TCollection : ICollection<TValue>
    {
        private TCollection collection;
    }
    

    I think this is what you’re looking for. It seems like maybe you’re trying to wrap a generic collection, and you want the first type of parameter to be an existing collection of the same type. This will give you exactly that.

    Hard to say, but you might not even need that. Unless you really need to know the underlying type of the TCollection object, you could probably just make it a TSOC<TValue> and accept an ICollection<TValue> to wrap it with:

    class TSOC<TValue> : ICollection<TValue>, INotifyCollectionChanged
    {
        private ICollection<TValue> collection;
    }
    

    This is functionally the same thing, you just won’t have knowledge of the actual underlying type of the ICollection<TValue>.


    Update:

    Responding now to this edit:

    Basically I just wanted to wrap all the ICollection methods in locks and force them to run on a particular thread, but still keep all the additional functionality of the programmer’s collection of choice…. I’m not sure this is possible though

    I think you actually could achieve what you want, but perhaps in a slightly different way than you expect. You don’t even really need the collection, you just want a synchronized wrapper:

    public class Synchronized<T>
    {
        private readonly object sync = new Object();
    
        private T item;
    
        public Synchronized(T item)
        {
            this.item = item;
        }
    
        public void Execute(Action<T> action)
        {
            lock (sync)
                action(item);
        }
    
        public TResult Evaluate<TResult>(Func<T, TResult> selector)
        {
            lock (sync)
                return selector(item);
        }
    }
    

    Then use it as:

    var sc = new Synchronized<ICollection<int>>(new List<int>());
    sc.Execute(c => c.Add(3));
    int count = sc.Evaluate(c => c.Count);
    

    Now I know this isn’t exactly what you want, since it doesn’t actually give you an instance of ICollection<T> you can pass around. But it’s a clean encapsulation of the locking behaviour and hides the inner collection, so you can guarantee that every operation is “thread safe.”

    If you really, really need an equivalent instance of ICollection<T> that wraps another ICollection<T> while adding thread safety, then what you’re actually looking for is method interception using a library like Castle DynamicProxy.

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

Sidebar

Related Questions

Is there anyway I can create a not in clause like I would have
Is there anyway I can make the process of adding references to C# projects
Is there any way I can detect when my page has been set as
Is there any way I can specify a standard or custom numeric format string
Is there any way we can convert text to speech in an iPhone app?
Is there any way I can vary caching by a controller action parameter using
Is there any way i can access the page object from within the global.asax
If I create an application on my Mac, is there any way I can
Is there any way that I can programmatically create (and I guess access) hidden
Is there any way that I can change how a Literal of a code

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.