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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:16:25+00:00 2026-05-16T12:16:25+00:00

When I create entities in a class library, I tend to add a Tag

  • 0

When I create entities in a class library, I tend to add a Tag property which may be used by users and extending libraries to store arbitrary data with the object. Now I am looking for the best way to implement such a Tag property in my class library, and the reasons why.

In System.Windows.Forms, Microsoft uses object Tag { get; set; } as the signature, but it looks too limiting as only one may use the Tag at any time.

I also thought about HashTable Tag { get; } to allow anyone to set and retrieve any data by key, but it seems too ‘exposing’ for a class library. Then IDictionary Tag { get; } would be a better option, but both allow anyone to clear the entire dictionary which I want to avoid. Then again, both HashTable and IDictionary only allow you to work with object instances, where something generic may be a better choice. But Dictionary<?> Tag { get; } is obviously not going to work for all possible consumers.

So, which way to go?

Edit:
As Timwi below correctly suggested, I don’t want different user classes to be able to interfere with eachother. I assume a scenario where there are many entity classes and only a few classes which want to store associated data.

  • 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-16T12:16:25+00:00Added an answer on May 16, 2026 at 12:16 pm

    It appears that one of your requirements is that the individual threads or processes that access an entity object should not be able to see each other’s private data. I’m afraid this is a pretty good indication that the private data should not be in the entity object which is shared by them. Instead, each process should have its own private data. It doesn’t have to be actually in the entity object itself, but it can be associated with the entity object via a private dictionary:

    private Dictionary<Entity, object> myPrivateEntityData = new ...;
    

    Furthermore, since this is now private to the process, and the process presumably knows exactly what kind of data it wants to associate with each Entity, you can now make it completely typesafe.

    private Dictionary<Entity, SuperSecretSpecialData> myPrivateEntityData = new ...;
    
    private sealed class SuperSecretData {
        public bool     NoOne;
        public int      ElseBut;
        public string   MeCan;
        public DateTime SeeThis;
    }
    

    There is some pain associated with having to check whether each entity is in the dictionary first, but you can fix this too. Personally I use a class AutoDictionary which simply extends Dictionary in such a way that it automatically creates objects when they are missing:

    public sealed class AutoDictionary<TKey, TVal> : Dictionary<TKey, TVal> where TVal : class, new()
    {
        public new TVal this[TKey key]
        {
            get
            {
                if (!ContainsKey(key))
                    Add(key, new TVal());
                return base[key];
            }
            set
            {
                base[key] = value;
            }
        }
    }
    
    private AutoDictionary<Entity, SuperSecretSpecialData> myPrivateEntityData = new ...;
    

    Then you can easily access myPrivateEntityData[someEntity].NoOne (etc.) and it will just work and never throw an exception because the key is not there, and yet you can still use ContainsKey if you want to check if it’s there.

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

Sidebar

Related Questions

I have an admin form that lets users create entities that require an image.
Users in our application participate in forums and create various entities. In the schema
I want to create a custom property on one of my entities mapped from
I have two entities which are Student and Class entities. Student and Class are
I'm trying to display a class object in a Create view, where a property
I've created two entities (simplified) in C#: class Log { entries = new List<Entry>();
I am trying to create entities out of a SQLite database. SQLite doesnt have
When I create two entities with many to many relationship, it will generate a
We have to create a historical log of all the changed entities. we have
First, we create classes that represent db entities, ok, done. Let's say we use

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.