Sobes.tech
Junior — Middle

Kas saaksite selgitada, mis on IReadOnlyList liides C#-s ja kuidas seda kasutada?

sobes.tech AI

Vastus AI-lt

In C#, the IReadOnlyList<T> interface represents a collection of elements that can be accessed via an index, but its contents cannot be modified (i.e., adding, removing, or changing elements). This ensures data safety when passing a list for reading without the risk of modification.

Example of usage:

IReadOnlyList<int> numbers = new List<int> {1, 2, 3};
int first = numbers[0]; // access by index
// numbers[0] = 10; // compilation error, as the collection is read-only

Therefore, IReadOnlyList<T> is convenient to use when you need to provide access to data without the ability to modify it.