icollection

Unit-testing IList with CollectionAssert

守給你的承諾、 提交于 2019-12-01 13:58:32
问题 The MSTest framework has a CollectionAssert that accepts ICollections. My method returns an IList. Apparently a list is not a collection.. Are there ways to make my IList an ICollection? 回答1: You could call the ToArray() extension method on it - Array implements ICollection Edit: Also, while List<T> implements ICollection, IList<T> only implements ICollection<T> which does not implement ICollection, so if you know the item in the test is a List<T> , you should be able to cast it... 回答2: You

T in class? AddRange ICollection?

落花浮王杯 提交于 2019-12-01 04:26:07
I try to do static class, add to icollection but i got some issues i cant seem to overcome. that is how i get so i can pass a ICollection in the method? cause T is that say it can not be resolved. and then i wonder is there a way to do AddRange on icollection? i was thinking of something like this but maby i am way out of my mind with it? public static ICollection<T> add(this IEnumerable<T> list) { ICollection<T> collection = null; return collection.AddRange(list); } No, ICollection<T> doesn't have an AddRange method - and even if it did, you'd be trying to dereference null which will throw a

Why lock on Collection.SyncRoot instead of just lock the collection?

喜你入骨 提交于 2019-11-29 07:11:29
I'm trying to understand the point of the syncroot in ICollection. Why not just lock the collection? lock(myCollection) { //do stuff to myCollection } vs lock(myCollection.SyncRoot) { //do stuff to myCollection } Typically, if thread safety is a serious concern, I would avoid either of these options. A far better option is typically to maintain your own private variable , and lock on it in all methods where it's required - including the entire public API that accesses the collection. The real danger is that, by locking on a type that's exposed or could be exposed to the outside world, you

Difference between IEnumerable and IEnumerable<T>?

不问归期 提交于 2019-11-28 16:39:12
What is the difference between IEnumerable and IEnumerable<T> ? I've seen many framework classes implementing both these interfaces, therefore I would like to know what advantages one get by implementing both? Please have a look how they've been defined: public interface IEnumerable { [DispId(-4)] IEnumerator GetEnumerator(); } public interface IEnumerable<T> : IEnumerable { IEnumerator<T> GetEnumerator(); } As we see, IEnumerable<T> derives from IEnumerable , that means whatever IEnumerable has, IEnumerable<T> inherits, then why do we implement both instead of just IEnumerable<T> ? Is

Why lock on Collection.SyncRoot instead of just lock the collection?

六月ゝ 毕业季﹏ 提交于 2019-11-28 00:48:07
问题 I'm trying to understand the point of the syncroot in ICollection. Why not just lock the collection? lock(myCollection) { //do stuff to myCollection } vs lock(myCollection.SyncRoot) { //do stuff to myCollection } 回答1: Typically, if thread safety is a serious concern, I would avoid either of these options. A far better option is typically to maintain your own private variable , and lock on it in all methods where it's required - including the entire public API that accesses the collection. The

Using a list as a data source for DataGridView

送分小仙女□ 提交于 2019-11-27 04:22:06
I've extracted the setting names and their respective values out of a configuration file into an ordered dictionary. The dictionary contains keys and values which are of the ICollection class. I want to bind that data and display it in a DataGridView. I've tried copying the strings to arrays and displaying those arrays, but when I ran the program the columns were blank and it did not seem to be binded at all. I've also attempted to set the DataGridView source directly to one the ordered dictionary collections (keys or values), but that also did not result in anything I wanted; the columns were

How to hide some members of an interface

半城伤御伤魂 提交于 2019-11-26 22:25:18
问题 I would like to create a custom collection that implements ICollection . But I would like not to expose some memebers of ICollection like Clear method. How to achieve this? 回答1: You can implement the interface explicitly and have the implementation hidden: public class UrClass : ICollection { void ICollection.Clear() { ... } } The user can't call urClassInstance.Clear() directly, but they can call ((ICollection)urClassInstance).Clear() indirectly like this. 回答2: You could make it empty or

ICollection<T> Vs List<T> in Entity Framework

笑着哭i 提交于 2019-11-26 15:46:52
I only watched a few webcasts before I went head first in to designing a few Entity Framework applications. I really didn't read that much documentation and I feel like I am suffering for it now. I have been using List<T> in my classes, and it has worked great. Now I have read some documentation and it states that I should have been using ICollection<T> . I changed to this, and it didn't even cause a model context change. Is this because both List<T> and ICollection<T> inherit IEnumerable<T> , and that is what is actually required for EF? However, if this is the case, why doesn't the EF

Why use ICollection and not IEnumerable or List<T> on many-many/one-many relationships?

梦想的初衷 提交于 2019-11-26 11:26:50
I see this a lot in tutorials, with navigation properties as ICollection<T> . Is this a mandatory requirement for Entity Framework? Can I use IEnumerable ? What's the main purpose of using ICollection instead of IEnumerable or even List<T> ? Usually what you choose will depend on which methods you need access to. In general - IEnumerable<> (MSDN: http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx ) for a list of objects that only needs to be iterated through, ICollection<> (MSDN: http://msdn.microsoft.com/en-us/library/92t2ye13.aspx ) for a list of objects that needs

ICollection<T> Vs List<T> in Entity Framework

烈酒焚心 提交于 2019-11-26 04:36:43
问题 I only watched a few webcasts before I went head first in to designing a few Entity Framework applications. I really didn\'t read that much documentation and I feel like I am suffering for it now. I have been using List<T> in my classes, and it has worked great. Now I have read some documentation and it states that I should have been using ICollection<T> . I changed to this, and it didn\'t even cause a model context change. Is this because both List<T> and ICollection<T> inherit IEnumerable<T