![]() ![]() We have learned about different interfaces and we should think about our requirements for choosing any one from the above. How to Choose the Right Collection Interface We should choose the minimum required interface when exposing an API to different clients, in this way we allow flexibility to the client code and give them the choice to choose any implementation they want. We can pass the specialCharacter of List type to the CountSpecialCharacters().Īs List is a concrete class, when we expose a List instead of an interface, we couple the client to a specific implementation. We have a List of char types in which we can use all the functionalities of the above three interfaces. Let’s take the same CountSpecialCharacters() method: public int CountSpecialCharacters(List specialCharacters) We can use it whenever we want to have a generic list with a specific object type. Till now we have seen all the interfaces, but List is a concrete class. NET/C# content and get paid? > JOIN US! and When to Use It? Wanna join Code Maze Team, help us produce more awesome. The compiler let us pass the array to the method because other functionalities are well-implemented and can be used properly. but that can cause an issue since some methods like Add(), Insert() etc are not implemented. We may be tempted to pass an array to IList parameter as we did in the example. We pass the specialCharacter to the CountSpecialCharacters() which takes IList type as input and returns the count. NET/C# content and get paid? > JOIN US! lst = new List() ") Now, not only can we loop through the characters but we can also Add() elements into the specialCharacters: Also, instead of our iteration, we could use the Count property to show the number of elements inside the collection: We have the same CountSpecialCharacters() method but this time specialCharacters is of ICollection type. Let’s modify the above CountSpecialCharacters() method: public int CountSpecialCharacters(ICollection specialCharacters) ![]() But we still can’t perform any index-related operations. ICollection allows to add or remove elements in the collection which is not possible with IEnumerable. It offers all the functionalities of the IEnumerable and adds a few new functionalities to it. When the Main() caller calls GetEvenNumberUpToTen() then every time the control moves to the next yield and returns the value. var enu = new ImplementationOfIEnumerable() Ĭonsole.WriteLine($"IEnumerable With Yield:") įoreach (var num in enu.GetEvenNumberUpToTen()) The caller Main() method calls GetEvenNumberUpToTen() to get even numbers. NET/C# content and get paid? > JOIN US! GetEvenNumberUpToTen() ![]() The yield keyword allows doing custom iteration while maintaining the state over a collection of items. There is another way to create IEnumerable types (other than instantiating a concrete type like a List for example) and that’s using the yield keyword. That’s why we can use foreach with every type that implements IEnumerable (that also means all the collection types we will tackle in this article). The foreach uses GetEnumerator(), MoveNext() and Current to iterate over the collection hiding the complexity of Enumerators. To count the characters, we can use the for each loop which iterates over the specialCharacters. We have a CountSpecialCharacters() method that takes specialCharacters of IEnumerable type. Let’s understand this with an example: public int CountSpecialCharacters(IEnumerable specialCharacters) IEnumerator provides methods like MoveNext(), Reset() and Current property. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |