© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
8 replies
CrosRoad95

❔ Question about getter property of list

so, i have some class containg property of type:
List<string>
List<string>
, how can i thread-safe return this list as some kind of snapshot of current state of list?
right now i'm doing it in followoing way but i'm not sure if it okey
private readonly object _lock = new();
private readonly List<string> _strings = [];
public IReadOnlyList<string> Strings {
  get {
    lock(_lock)
      return [.._strings];
  }
}
private readonly object _lock = new();
private readonly List<string> _strings = [];
public IReadOnlyList<string> Strings {
  get {
    lock(_lock)
      return [.._strings];
  }
}
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

✅ Override (abstract) getter-only property with a setter
C#CC# / help
3y ago
Modify property of object in List [Answered]
C#CC# / help
4y ago
✅ sort list after property
C#CC# / help
3y ago
Question about enabling filtering of items
C#CC# / help
2y ago