Список только для чтения строк

Другое решение:

читает следующий ключ реестра:

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName

или другие ключи от

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion
7
задан abatishchev 3 October 2012 в 22:22
поделиться

2 ответа

Depends on your position. ReadOnlyCollection works very well when you have an internal modifiable list that needs to be given to some other code that isn't allowed to change the list. For simple code, using ReadOnlyCollection is probably overkill. If the code is large and there's a risk some other programmer (or you, down the road) might accidentally attempt to modify the list, then it might be worth it.

In short: don't use ReadOnlyCollection idiomatically, use it when it makes sense.

8
ответ дан 6 December 2019 в 10:52
поделиться

По возможности предпочитайте статическую проверку типа. IEnumerable доступен только для чтения.

IEnumerable<string> list = new List<string> { "type", "currency", "date", "amount" };

Последующий код не может изменить список, и ошибки будут обнаружены во время компиляции - если вы не используете отражение, но тогда отражение можно использовать для обхода проверок во время выполнения, как хорошо.

6
ответ дан 6 December 2019 в 10:52
поделиться
Другие вопросы по тегам:

Похожие вопросы: