![]() If I imagine that you are parsing a command-line string where order is important, I would stick with IList if I were you. It will therefore behave the same way regardless of the CommaSeparate() method's return type.Īs someone else pointed out, the FXCop rule warns against using List, not IList the question you linked to is asking why FXCop doesn't recommend using IList in place of List, which is another matter. Regardless of the runtime type of the object returned, consider the behavior when the static reference is IList or ICollection: When you call GetEnumerator() (perhaps implicitly in a foreach loop), you're going to call the same method and get the same object regardless of the static type of the reference. If you need the method to return, by contract, a type whose contract requires a certain order, then you should express that in the method's signature by returning the more specific type. For example, if you return the Values collection of a SortedDictionary, the objects will be in the the order defined by the dictionary's comparer. The objects will be in the order specified by the object returned. The ICollection interface doesn't specify anything about an order. In the case of an API contract, to indicate order, which of the following is correct: IEnumerable Grob(string s) It is critical that the order of items be maintained.Ĭonceptually, i require a contract that indicates an "ordered" set of string tuples. This begs the wider question: Does an ICollection even have an order?Ĭonceptually, imagine i want to parse a command line string. Is there a ways to access items of a collection in order? Unfortunately, my internal implementation constructs an array which i can be cast to return IList or ICollection, but not as a Collection. Implements IEnumerable(Of Box).Following the rules that a public APIs should never return a list, i'm blinding converting all code that returned lists, to return ICollection instead: public IList CommaSeparate(String value) ![]() Public Function GetEnumerator() As IEnumerator(Of Box) _ ' the non-generic IEnumerable method is explicitly implemented. ' be used with the non-generic IEnumerator. ' The generic enumerator obtained from IEnumerator by GetEnumerator can also ![]() Public Class BoxCollection : Implements ICollection(Of Box) Public Overrides Function GetHashCode() As Integer ![]() Public Overrides Function Equals(ByVal obj As Object) As Boolean In this article, I am going to give you a brief Introduction to Collections in C.Please read our previous article where we discussed the Advantages and Disadvantages of Arrays in C with Examples. Public Overloads Function Equals(ByVal other As Box) As Boolean Implements IEquatable(Of Box).Equals Back to: C.NET Tutorials For Beginners and Professionals Introduction to Collections in C. Public Sub New(ByVal h As Integer, ByVal l As Integer, ByVal w As Integer) Public Class Box : Implements IEquatable(Of Box) using System īoxCollection bxList = new BoxCollection() Ĭonsole.WriteLine("Contains ", bx.Height.ToString(), bx.Length.ToString(), bx.Width.ToString(), bx.GetHashCode().ToString()) This example also implements an IEnumerator interface for the BoxCollection class so that the collection can be enumerated. The BoxCollection class also provides an overload of the Contains method that takes a specified Equalit圜omparer object, such as BoxSameDimensions and BoxSameVol classes in the example. This method is used by the Add method so that each Box added to the collection has a unique set of dimensions. The BoxCollection class implements the Contains method to use the default equality to determine whether a Box is in the collection. The Box class implements the IEquatable interface to define the default equality as the dimensions being the same. Equality can be defined as all dimensions being the same or the volume being the same. Each Box has height, length, and width properties, which are used to define equality. The following example implements the ICollection interface to create a collection of custom Box objects named BoxCollection.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |