{"id":616,"date":"2023-09-18T00:02:52","date_gmt":"2023-09-18T06:02:52","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=616"},"modified":"2023-09-18T00:03:10","modified_gmt":"2023-09-18T06:03:10","slug":"icollection-in-dotnet","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/icollection-in-dotnet\/","title":{"rendered":"ICollection in dotnet"},"content":{"rendered":"\n<p>In .NET, <code>ICollection<\/code> is an interface that represents a non-generic collection of objects that can be individually accessed by index (like an array) and allows for adding, removing, and checking for the existence of items. It is part of the <code>System.Collections<\/code> namespace and serves as the base interface for several collection types in .NET, including <code>List&lt;T&gt;<\/code>, <code>HashSet&lt;T&gt;<\/code>, and <code>Queue&lt;T&gt;<\/code>.<\/p>\n\n\n\n<p>Here are some of the main characteristics and members of the <code>ICollection<\/code> interface:<\/p>\n\n\n\n<ol>\n<li><strong>Count Property<\/strong>: <code>ICollection<\/code> has a <code>Count<\/code> property that returns the number of elements in the collection.<\/li>\n\n\n\n<li><strong>IsReadOnly Property<\/strong>: It includes an <code>IsReadOnly<\/code> property that indicates whether the collection is read-only or not. A read-only collection allows elements to be read but not modified.<\/li>\n\n\n\n<li><strong>Add Method<\/strong>: The <code>Add<\/code> method allows you to add an item to the collection.<\/li>\n\n\n\n<li><strong>Remove Method<\/strong>: The <code>Remove<\/code> method allows you to remove a specific item from the collection.<\/li>\n\n\n\n<li><strong>Contains Method<\/strong>: The <code>Contains<\/code> method checks whether a specific item is present in the collection.<\/li>\n\n\n\n<li><strong>Clear Method<\/strong>: The <code>Clear<\/code> method removes all items from the collection.<\/li>\n\n\n\n<li><strong>CopyTo Method<\/strong>: The <code>CopyTo<\/code> method copies the elements of the collection to an array, starting at a particular index.<\/li>\n\n\n\n<li><strong>GetEnumerator Method<\/strong>: It includes an <code>GetEnumerator<\/code> method that returns an <code>IEnumerator<\/code> object, which can be used to iterate through the elements of the collection.<\/li>\n<\/ol>\n\n\n\n<p>Here&#8217;s an example of how you might use <code>ICollection<\/code>:<\/p>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#5E69FF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">using System;\nusing System.Collections;\nusing System.Collections.Generic;\n\nclass Program\n{\n    static void Main()\n    {\n        ICollection&lt;int> numbers = new List&lt;int>();\n\n        \/\/ Adding elements\n        numbers.Add(1);\n        numbers.Add(2);\n        numbers.Add(3);\n\n        \/\/ Count\n        Console.WriteLine(\"Count: \" + numbers.Count);\n\n        \/\/ Contains\n        bool containsTwo = numbers.Contains(2);\n        Console.WriteLine(\"Contains 2: \" + containsTwo);\n\n        \/\/ Remove\n        numbers.Remove(2);\n        Console.WriteLine(\"Count after removing 2: \" + numbers.Count);\n\n        \/\/ Enumerate\n        foreach (int number in numbers)\n        {\n            Console.WriteLine(number);\n        }\n    }\n}<\/code><\/pre><\/div><\/div>\n\n\n\n<p>In this example, <code>ICollection<\/code> is used to manage a collection of integers. You can see how elements are added, removed, and checked for existence using the methods provided by the interface.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In .NET, ICollection is an interface that represents a non-generic collection of objects that can be individually accessed by index (like an array) and allows for adding, removing, and checking for the existence of items. It is part of the System.Collections namespace and serves as the base interface for several collection types in .NET, including [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":476,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[39,41,1,51,90],"tags":[],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/616"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=616"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"predecessor-version":[{"id":617,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/616\/revisions\/617"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/476"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}