{"id":185,"date":"2016-03-02T12:29:51","date_gmt":"2016-03-02T12:29:51","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=185"},"modified":"2023-04-25T12:32:37","modified_gmt":"2023-04-25T11:32:37","slug":"using-list","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/memory\/list\/using-list","title":{"rendered":".Using List"},"content":{"rendered":"\n<p>Unlike arrays, the List collection type resizes dynamically. &nbsp;If you are working with data where you would need to be resizing an array a lot then use a list instead. &nbsp;It is ideal for linear collections that you don&#8217;t need to adjust using an index.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Good resources<\/h5>\n\n\n\n<p><a href=\"http:\/\/www.dotnetperls.com\/list\" target=\"_blank\" rel=\"noopener\">http:\/\/www.dotnetperls.com\/list<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">You need this namespace<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections.Generic;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Declaring<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tList&lt;byte&gt; LogEvents = new List&lt;byte&gt;();\n\t\/\/or for a class:\n\tList&lt;LogEventClass&gt; LogEvents = new List&lt;LogEventClass&gt;();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Adding Objects<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tLogEventClass LogEvent1 = new LogEventClass();\n\tLogEvents.Add(LogEvent1);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Add Array<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tLogEvents.AddRange(System.Text.Encoding.UTF8.GetBytes(NameText));<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Removing Objects<\/h4>\n\n\n\n<p>Find the object index then<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tLogEvents.RemoveAt(Index);<\/code><\/pre>\n\n\n\n<p>There is also RemoveRange or just Remove where you specify an exact match for it to find and remove.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Total Number Of Objects In A List<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tfor (Index = 0; Index &lt; LogEvents.Count; Index++)\n\t{\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Reading A List Item<\/h4>\n\n\n\n<p>Use an index as with an array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tSomeVariable = LogEvents&#91;Index];<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Working Through A List<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tforeach (LogEventClass LogEvent in LogEvents)\n\t{\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Does List contain a value?<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tif (MyList.Contains(123))\n\t{\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Clear A List<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tLogEvents.Clear();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Convert List To An Array<\/h4>\n\n\n\n<p>Use .ToArray();<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Copy An Array To A List<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tList&lt;LogEventClass&gt; LogEvents = new List&lt;LogEventClass&gt;(SomeArray);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Unlike arrays, the List collection type resizes dynamically. &nbsp;If you are working with data where you would need to be resizing an array a lot then use a list instead. &nbsp;It is ideal for linear collections that you don&#8217;t need to adjust using an index. Good resources http:\/\/www.dotnetperls.com\/list You need this namespace Declaring Adding Objects [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-list"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":8,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":1305,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/185\/revisions\/1305"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}