Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent List changes in C#

I have a Linkedlist of objects (can be changed to any other Collection type as long as it keeps entry order) in assembly A (DLL).

When Assembly B creates an object from the class that contains said LinkedList, it assigns a method to report back some information. One of the arguments that the method takes is said LinkedList.

Now i want this method in Assembly B to be able to read from that LinkedList (traverse it and do stuff based on it's content), however i need to prevent that method from changing the data in the List.

While i am the one using Assembly A right now, When and if it goes public, I need to prevent the data from being changed from outside my assembly so that no 3rd party using the library can fidget with results within the assembly.

Basicly i am looking to 'seal' the parameter that brings that LinkedList from Assembly A to Assembly B

Am I making any sense?

like image 819
537mfb Avatar asked Dec 06 '22 12:12

537mfb


1 Answers

new List<T>(linkedList).AsReadOnly();
like image 50
abatishchev Avatar answered Dec 10 '22 03:12

abatishchev