简体   繁体   中英

Need help understanding "readonly" in C#

I'm trying to get my head around this. What's the difference between these 4?

 1. private ObservableCollection<T> _myObservableCollestionOfTs
 2. private readonly ObservableCollection<T> _myObservableCollestionOfTs
 3. private ReadOnlyObservableCollection<T> _myObservableCollestionOfTs
 4. private readonly ReadOnlyObservableCollection<T> _myObservableCollestionOfTs

Thank you for any help :)

  1. A mutable reference to a mutable collection. The collection's elements can be changed, and the reference pointing to the collection can be reassigned to a different collection or set to null.

  2. An immutable reference to a mutable collection. The collection's elements can be changed, but the reference pointing to the collection cannot.

  3. A mutable reference to an immutable collection. The collection's elements cannot be changed, but the reference can be pointed to a different collection.

  4. An immutable reference to an immutable collection. Neither the collection's elements nor the reference pointing to the collection can be changed.

Read-only collections are simply wrapper classes around ordinary collections that prevent modification of the collection's elements; see the remarks here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM