© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
5 replies
Kiel

✅ HashSet property with List backing field

Posting here instead of in #database because it's not really related to db-specific stuff.

For reasons described vaguely here: https://github.com/npgsql/efcore.pg/issues/2301, I am looking to implement a
HashSet<T>
HashSet<T>
property with a
List<T>
List<T>
backing field which updates appropriately as I add and remove objects from the publicly exposed
HashSet<T>
HashSet<T>
.
example class:
public sealed class GlobalUser
{
    private List<Snowflake> _blacklistedHighlightUsers = null!;

    public HashSet<Snowflake> BlacklistedHighlightUsers
    {
        get { /* TODO */ }
        set { /* TODO */ }
    }
}
public sealed class GlobalUser
{
    private List<Snowflake> _blacklistedHighlightUsers = null!;

    public HashSet<Snowflake> BlacklistedHighlightUsers
    {
        get { /* TODO */ }
        set { /* TODO */ }
    }
}

I want the underlying list field to be updated every time I modify the public property. Is there a way to accomplish this without any of the following?
- never calling Add/Remove/etc methods and instead manually re-assigning the
BlacklistedHighlightUsers
BlacklistedHighlightUsers
every time
- implementing my own wrapper class which overrides the Add/Remove/etc methods to somehow write to this underlying list
- giving up on doing this in the first place and using a public
List<T>
List<T>
and tossing on
.ToHashSet().ToList()
.ToHashSet().ToList()
or
.Distinct().ToList()
.Distinct().ToList()
everywhere I go

all are what I'd consider ugly, hacky solutions that I'm not a fan of as they either mean I am overcomplicating things or there really just is not a solution
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Backing Field and Primary Constructors
C#CC# / help
11mo ago
Design Hashset
C#CC# / help
2y ago
❔ Property vs field?
C#CC# / help
3y ago
Understanding get; set; backing field requirements.
C#CC# / help
4mo ago