What is the function of Asreadonly() in C#?

Asked

Viewed 114 times

8

I’m studying List in C#, I searched on the internet but still did not understand how it works.

List<string> list = new List<string>();

list.Add("Maria");
list.Add("Lucas");
list.Add("Renata");
list.Add("Yara");
list.Add("Josias");
list.Add("Morten Granau");
list.Add("Phaxe")

IList<string> auxiliar = list.AsReadOnly();

What that function does AsReadOnly()?

2 answers

8


Its function is to create an object (ReadOnlyCollection) that guarantees the contract that the original object will not be changed, this way you can pass to methods, possibly in another execution context (other thread) and it will be guaranteed that it will be used only as reading. So it’s a more semantic and secure way of using the object you want in this context to be immutable.

Note that it does not make any copy, does not have a real processing, just keeps an object that is of the same type as the original without allowing changes to its content. It usually only makes sense if you’re moving on to another method.

The whole. NET was modified to accept this type of object and give more guarantees, and its codes should do the same, after all, is safer in all directions and even be more performatic, especially help in competing environments.

If you want the original object to be immutable you should always use a ImmutableList which is part of a collection of objects that people don’t know about and should be chosen if you don’t need to make changes to it. Unfortunately most programmers do not know the novelties of C# and . NET and continue to do as it was 15 years ago (so I have a talk and plan to one day have a book on C# Modern).

Documentation.

There’s no point doing it the way you did it, do it this way:

var list = new List<string> { "Maria", "Lucas", "Renata", "Yara", "Josias", "Morten Granau""Morten Granau", "Phaxe" };
Metodo(list.AsReadOnly());

I put in the Github for future reference.

Whereas the signature of this method is more or less Metodo(ReadOnlyCollection lista).

In many cases the use of a IReadOnlyCollection may be sufficient.

7

It returns a class called ReadOnlyCollection. With this, you guarantee that the collection will not be changed, it will actually be read only.

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Add(4);
myList.Add(5);

var myListReadOnly = myList.AsReadOnly();
myListReadOnly[0] = 1;// não pode fazer isto
myListReadOnly.Add(1); // não pode fazer isto

In the example you put

IList<string> auxiliar = list.AsReadOnly();

It doesn’t make sense because you’re creating a ReadOnlyCollection<T> but converting again to a IList<T>, i.e., by copying a list that can be changed. This conversion is possible because ReadOnlyCollection<T> also implements IList<T>
The right thing would be:

var auxiliar = list.AsReadOnly(); 

It would be implicit the use of the Readonlycollection class. Or you can do it explicitly:

IReadOnlyList<string> auxiliar = list.AsReadOnly();

An important detail is that ReadOnlyCollection is just a wrapper that does not expose the collection modification methods but if any changes are made to the original list, these same changes will be reflected in this list as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.