readonly-collection

How can I access a element of a IReadOnlyCollection through it index?

情到浓时终转凉″ 提交于 2020-05-25 09:49:20
问题 I am working with selenium and I am using the function FindElements so I am getting a element that implements IReadOnlyCollection interface. I want to iterate through the list but it seems that IReadOnlyCollection doesnt have any method like Get(int index) or a implementation of the operation []. I want to avoid transforming the result to a List or to an array since I just want to access the elements to read them. Currently I don't want to use a foreach since I need to manage an index so I

How can I access a element of a IReadOnlyCollection through it index?

瘦欲@ 提交于 2020-05-25 09:48:45
问题 I am working with selenium and I am using the function FindElements so I am getting a element that implements IReadOnlyCollection interface. I want to iterate through the list but it seems that IReadOnlyCollection doesnt have any method like Get(int index) or a implementation of the operation []. I want to avoid transforming the result to a List or to an array since I just want to access the elements to read them. Currently I don't want to use a foreach since I need to manage an index so I

How can I access a element of a IReadOnlyCollection through it index?

跟風遠走 提交于 2020-05-25 09:48:07
问题 I am working with selenium and I am using the function FindElements so I am getting a element that implements IReadOnlyCollection interface. I want to iterate through the list but it seems that IReadOnlyCollection doesnt have any method like Get(int index) or a implementation of the operation []. I want to avoid transforming the result to a List or to an array since I just want to access the elements to read them. Currently I don't want to use a foreach since I need to manage an index so I

Readonly collection properties that NHibernate can work with

孤街醉人 提交于 2020-01-03 18:51:48
问题 My domain classes have collections that look like this: private List<Foo> _foos = new List<Foo>(); public virtual ReadOnlyCollection<Foo> Foos { get { return _foos.AsReadOnly(); } } This gives me readonly collections that can be modified from within the class (i.e. by using the field _foos). This collection is mapped as follows (Fluent NHibernate): HasMany(x => x.Foos).KeyColumn("ParentClassId").Cascade.All().Inverse().Access.CamelCaseField(Prefix.Underscore); Now when I try to use this

How to properly use IReadOnlyDictionary?

两盒软妹~` 提交于 2020-01-01 08:55:34
问题 From msdn: Represents a generic read-only collection of key/value pairs. However consider following: class Test { public IReadOnlyDictionary<string, string> Dictionary { get; } = new Dictionary<string, string> { { "1", "111" }, { "2", "222" }, { "3", "333" }, }; public IReadOnlyList<string> List { get; } = (new List<string> { "1", "2", "3" }).AsReadOnly(); } class Program { static void Main(string[] args) { var test = new Test(); var dictionary = (Dictionary<string, string>)test.Dictionary; /

How to properly use IReadOnlyDictionary?

╄→гoц情女王★ 提交于 2020-01-01 08:55:17
问题 From msdn: Represents a generic read-only collection of key/value pairs. However consider following: class Test { public IReadOnlyDictionary<string, string> Dictionary { get; } = new Dictionary<string, string> { { "1", "111" }, { "2", "222" }, { "3", "333" }, }; public IReadOnlyList<string> List { get; } = (new List<string> { "1", "2", "3" }).AsReadOnly(); } class Program { static void Main(string[] args) { var test = new Test(); var dictionary = (Dictionary<string, string>)test.Dictionary; /

ReadonlyCollection, are the objects immutable?

限于喜欢 提交于 2019-12-20 02:27:28
问题 I'm trying using ReadOnlyCollection to make object immutable, I want the property of object are immutable. public ReadOnlyCollection<FooObject> MyReadOnlyList { get { return new ReadOnlyCollection<FooObject>(_myDataList); } } But I little confused. I tried to change the property of the object in to MyReadOnlyList using a foreach and ... I can change value property, is it correct? I understood ReadOnlyCollection set an add level to make the object immutable. 回答1: The fact that

Using IReadOnlyCollection<T> instead of IEnumerable<T> for parameters to avoid possible multiple enumeration

你说的曾经没有我的故事 提交于 2019-12-19 05:35:08
问题 My question is related to this one concerning the use of IEnumerable<T> vs IReadOnlyCollection<T> . I too have always used IEnumerable<T> to expose collections as both return types and parameters because it benefits from being both immutable and lazily executed. However, I am becoming increasingly concerned about the proliferation of places in my code where I must enumerate a parameter to avoid the possible multiple enumeration warning that ReSharper gives. I understand why ReSharper suggests

Using IReadOnlyCollection<T> instead of IEnumerable<T> for parameters to avoid possible multiple enumeration

耗尽温柔 提交于 2019-12-19 05:35:08
问题 My question is related to this one concerning the use of IEnumerable<T> vs IReadOnlyCollection<T> . I too have always used IEnumerable<T> to expose collections as both return types and parameters because it benefits from being both immutable and lazily executed. However, I am becoming increasingly concerned about the proliferation of places in my code where I must enumerate a parameter to avoid the possible multiple enumeration warning that ReSharper gives. I understand why ReSharper suggests

ReadOnlyCollection or IEnumerable for exposing member collections?

为君一笑 提交于 2019-12-17 02:28:09
问题 Is there any reason to expose an internal collection as a ReadOnlyCollection rather than an IEnumerable if the calling code only iterates over the collection? class Bar { private ICollection<Foo> foos; // Which one is to be preferred? public IEnumerable<Foo> Foos { ... } public ReadOnlyCollection<Foo> Foos { ... } } // Calling code: foreach (var f in bar.Foos) DoSomething(f); As I see it IEnumerable is a subset of the interface of ReadOnlyCollection and it does not allow the user to modify