C#C
C#3y ago
CrosRoad95

❔ Question about getter property of list

so, i have some class containg property of type: 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];
  }
}
Was this page helpful?