<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Paula Wojciechowska</title>
    <description>The latest articles on DEV Community by Paula Wojciechowska (@wojciechowskapaula).</description>
    <link>https://dev.to/wojciechowskapaula</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F836590%2Feaa61f3e-f79e-4f65-a9ce-d88ac0754d7a.jpeg</url>
      <title>DEV Community: Paula Wojciechowska</title>
      <link>https://dev.to/wojciechowskapaula</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wojciechowskapaula"/>
    <language>en</language>
    <item>
      <title>Dictionary implementation in C#</title>
      <dc:creator>Paula Wojciechowska</dc:creator>
      <pubDate>Mon, 28 Mar 2022 13:29:17 +0000</pubDate>
      <link>https://dev.to/wojciechowskapaula/dictionary-implementation-in-c-50j1</link>
      <guid>https://dev.to/wojciechowskapaula/dictionary-implementation-in-c-50j1</guid>
      <description>&lt;p&gt;In the previous post we explained the &lt;a href="https://dotnetos.org/blog/2022-03-07-list-implementation/"&gt;implementation details&lt;/a&gt; of &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt;. This time we will look at another generic collection defined in &lt;code&gt;System.Collection.Generic&lt;/code&gt; namespace which is &lt;code&gt;Dictionary&amp;lt;TKey TValue&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;The most important implementation elements of the &lt;code&gt;Dictionary&amp;lt;TKey, TValue&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;buckets&lt;/code&gt; - set of elements with similar hashes &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;entries&lt;/code&gt; - elements of the &lt;code&gt;Dictionary&amp;lt;TKey, TValue&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;freeList&lt;/code&gt; - index of the first free place&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;freeCount&lt;/code&gt; - the number of empty spaces in the array, not at the end&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;count&lt;/code&gt; - the number of elements that are currently in the &lt;code&gt;Dictionary&amp;lt;TKey, TValue&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;version&lt;/code&gt; - changes as the &lt;code&gt;Dictionary&amp;lt;TKey, TValue&amp;gt;&lt;/code&gt; is modified&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jb47BIxw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5al3sbwa9yct8d3ar7cd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jb47BIxw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5al3sbwa9yct8d3ar7cd.jpg" alt="Dictionary Implementation" width="880" height="674"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;...and a few more equally important elements that &lt;code&gt;Entry&lt;/code&gt; contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;_key&lt;/code&gt; - key to identify element with &lt;code&gt;TKey&lt;/code&gt; type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;_value&lt;/code&gt; - value of an element with &lt;code&gt;TValue&lt;/code&gt; type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;_hashCode&lt;/code&gt; - numeric value used to identify an object in hash-based collection&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;_next&lt;/code&gt; - describes the next item in the &lt;code&gt;bucket&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AETgNv6Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qstz5qdc33hafc3idtke.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AETgNv6Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qstz5qdc33hafc3idtke.png" alt="Entry" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The dictionary uses an array of &lt;code&gt;Entry&lt;/code&gt; structures to store data. To get a better understanding of how this really works, you need to know what exactly a hash table is.&lt;br&gt;
&lt;strong&gt;Hash table&lt;/strong&gt; (also called hash map) is a data structure that implements an associative array which allows you to store pairs - each pair contains a key and a value.&lt;br&gt;
&lt;strong&gt;With the key, we are able to quickly find the value associated with the key.&lt;/strong&gt; According to the dictionary's equality comparer the key is unique within the entire associative array.&lt;br&gt;
In .NET the hash table contains a list of &lt;code&gt;buckets&lt;/code&gt; to store values. A hash table uses a &lt;strong&gt;hash function&lt;/strong&gt; to compute an index based on the key. This allows us, for example, to find the correct &lt;code&gt;bucket&lt;/code&gt; with the value we are looking for.&lt;br&gt;
The same is the situation with other operations - by calculating the hashcode we can add an element to the appropriate &lt;code&gt;bucket&lt;/code&gt; with the index designated for it. We will continue to explain this in more detail later in this post.&lt;br&gt;
The value in the &lt;code&gt;bucket&lt;/code&gt; indicates the index in &lt;code&gt;entries&lt;/code&gt; +1. As it is in infographic: a value of 3 points to index 2. And a value of 2 points to index 1.&lt;br&gt;
The &lt;code&gt;next&lt;/code&gt; property points to the next item that is in the same bucket. In the picture it is additionally marked with an arrow. When &lt;code&gt;next&lt;/code&gt; is equal to -1, it means that it is the last item in the &lt;code&gt;bucket&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding an item when the key doesn't exist
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--in0zr4hn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzh9hc3rkc4zv4z9ukm3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--in0zr4hn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzh9hc3rkc4zv4z9ukm3.jpg" alt="DictionaryKeyDoesNotExist" width="880" height="1528"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the previous section, we mentioned that the key must be unique. Now let's look at an example in which we want to add a new &lt;code&gt;Entry&lt;/code&gt; to our list. When the key doesn't exist and we have one free space not at the end of the array. The first operation in this case is to compute &lt;code&gt;hashcode&lt;/code&gt; and find a suitable &lt;code&gt;bucket&lt;/code&gt; using the formula: &lt;code&gt;hashCode&lt;/code&gt; % &lt;code&gt;buckets.Length&lt;/code&gt;. When we find this &lt;code&gt;bucket&lt;/code&gt;, we compare &lt;code&gt;hashCode&lt;/code&gt; of the new element we want to add to the array with the &lt;code&gt;hashCode&lt;/code&gt; of the first &lt;code&gt;Entry&lt;/code&gt;, then move on to the next one (pointed to by &lt;code&gt;next&lt;/code&gt;) and repeat the comparison.&lt;br&gt;
&lt;strong&gt;If none of the existing &lt;code&gt;hashCodes&lt;/code&gt; are the same then we add a new element to the first empty space.&lt;/strong&gt; Our &lt;code&gt;version&lt;/code&gt; grows and the value of &lt;code&gt;bucket&lt;/code&gt; points to the last added element. If the modulo result points to a &lt;code&gt;bucket&lt;/code&gt; that already contains an item (we call this situation a hash collision), after adding a new element its index from the &lt;code&gt;entries&lt;/code&gt; array is set to the value of &lt;code&gt;bucket&lt;/code&gt; and the &lt;code&gt;next&lt;/code&gt; field is set to point to the previous item, resulting in a chain of items.☝️&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding an item when the key exist
&lt;/h2&gt;

&lt;p&gt;Let's look at the case where we want to change the value of an existing &lt;code&gt;Entry&lt;/code&gt;. In the first steps, nothing changes from the previous example - we calculate the &lt;code&gt;hashCode&lt;/code&gt; and find the appropriate &lt;code&gt;bucket&lt;/code&gt;. After that, the &lt;code&gt;hashCodes&lt;/code&gt; of the elements in the &lt;code&gt;buckets&lt;/code&gt; are compared. When we have verified that the &lt;code&gt;hashCode&lt;/code&gt; of the new &lt;code&gt;Entry&lt;/code&gt; is the same as the existing one, keys comparision occurs. If the keys are the same, &lt;strong&gt;value is overwritten&lt;/strong&gt; and the &lt;code&gt;version&lt;/code&gt; is incremented by 1. It's so simple and interesting at the same time! ✨&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mtrUxRT2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kx59d26ygr1d41ecvmfe.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mtrUxRT2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kx59d26ygr1d41ecvmfe.jpg" alt="DictionaryKeyExist" width="880" height="1740"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Resize
&lt;/h2&gt;

&lt;p&gt;Let's look at another interesting situation. If we want to add an element to the dictionary and there is no more space in it, before this operation we should resize the array. &lt;strong&gt;The first step is to create an empty enlarged array whose size is equal to the nearest Prime Number of doubled the initial size of the array.&lt;/strong&gt; We use Prime Numbers to minimalize probability of hash collisions. The next step is to copy the &lt;code&gt;entries&lt;/code&gt; and calculate the &lt;code&gt;hashcode&lt;/code&gt; for the new element and find the right &lt;code&gt;bucket&lt;/code&gt;. Then, as in the previous examples, add the element to the first free spot of the &lt;code&gt;entries&lt;/code&gt; array.👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0oclYs-R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wz3ysvoc7o8oloagzfau.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0oclYs-R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wz3ysvoc7o8oloagzfau.jpg" alt="ResizeDictionary" width="880" height="1392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Remove
&lt;/h2&gt;

&lt;p&gt;We've already learned how adding elements to the dictionary looks like, it's high time to know the implementation details of removing them. The initial steps remain the same - the &lt;code&gt;hashCode&lt;/code&gt; is calculated and the appropriate &lt;code&gt;bucket&lt;/code&gt; is found. Then a comparison of &lt;code&gt;hashCodes&lt;/code&gt; takes place and a check is made to see if the keys are the same.&lt;br&gt;
After that, the key is removed and the &lt;code&gt;version&lt;/code&gt; grows. &lt;strong&gt;When an element from the array is removed, the space it occupies goes into the chain &lt;code&gt;freelist&lt;/code&gt;&lt;/strong&gt;. The Dictionary stores the index of the next element using the &lt;code&gt;next&lt;/code&gt; property of the Entry structure. This way we know, in case we want to add a new element after deletion, which space it will occupy first - &lt;code&gt;entry[1]&lt;/code&gt; and when adding one more element in turn - &lt;code&gt;entry[0]&lt;/code&gt;.👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YEQBp70g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lz6acqyhqff3kumoku61.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YEQBp70g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lz6acqyhqff3kumoku61.jpg" alt="DictionaryRemove" width="880" height="1741"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you enjoyed this post and want to keep learning more, check out our social channels💜&lt;br&gt;
&lt;a href="https://twitter.com/dotnetosorg"&gt;Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://discord.com/invite/X9FNmdVmyA"&gt;Discord&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.instagram.com/dotnetosorg/"&gt;Instagram&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Sources
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/microsoft/referencesource/blob/master/mscorlib/system/collections/generic/dictionary.cs"&gt;https://github.com/microsoft/referencesource/blob/master/mscorlib/system/collections/generic/dictionary.cs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.microsoft.com/pl-pl/dotnet/api/system.collections.generic.dictionary-2?view=net-6.0"&gt;https://docs.microsoft.com/pl-pl/dotnet/api/system.collections.generic.dictionary-2?view=net-6.0&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
    </item>
    <item>
      <title>A few words about the implementation of List&lt;T&gt; in C#</title>
      <dc:creator>Paula Wojciechowska</dc:creator>
      <pubDate>Fri, 25 Mar 2022 09:31:46 +0000</pubDate>
      <link>https://dev.to/wojciechowskapaula/a-few-words-about-the-implementation-of-list-in-c-2h8j</link>
      <guid>https://dev.to/wojciechowskapaula/a-few-words-about-the-implementation-of-list-in-c-2h8j</guid>
      <description>&lt;p&gt;In C# &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; is a generic collection that is used to store any number of strongly typed objects as a list, where T is the type of objects. It allows us to perform a number of operations to find individual list items and modify them with operations such as adding, deleting or sorting.&lt;br&gt;
The &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; class implements: &lt;code&gt;ICollection&amp;lt;T&amp;gt;&lt;/code&gt;, &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt;, &lt;code&gt;IList&amp;lt;T&amp;gt;&lt;/code&gt;, &lt;code&gt;IReadOnlyCollection&amp;lt;T&amp;gt;&lt;/code&gt;, &lt;code&gt;IReadOnlyList&amp;lt;T&amp;gt;&lt;/code&gt;, &lt;code&gt;ICollection&lt;/code&gt;, &lt;code&gt;IEnumerable&lt;/code&gt; and &lt;code&gt;IList&lt;/code&gt; interface. &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; class is defined in the &lt;code&gt;System.Collection.Generic&lt;/code&gt; namespace.&lt;/p&gt;

&lt;p&gt;If we look deeper, internally the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; stores all elements as a reference to a single array of elements of T type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inside of List
&lt;/h2&gt;

&lt;p&gt;The most important implementation elements of the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;items&lt;/code&gt; - elements of the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;size&lt;/code&gt; - the number of items that are currently in the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;version&lt;/code&gt; - changes as the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; is modified&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aNid2IVe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtygqxdrgi4hdg7sj32t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aNid2IVe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtygqxdrgi4hdg7sj32t.png" alt="List Implementation" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The List implementation uses an underlying array for storing items. This underlying array length is called Capacity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding an item to a List
&lt;/h2&gt;

&lt;p&gt;Let's analyze the first operation that adds items to the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt;. For a complete explanation, you will need to look at Capacity in depth. &lt;strong&gt;The Capacity value for the default constructor is equal to 4&lt;/strong&gt;. Even if you add less elements as in our example - there is room for 4 elements in the internal array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N4kP3UNV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1fgjwisgoy3kbxzylu27.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N4kP3UNV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1fgjwisgoy3kbxzylu27.png" alt="Add elements to List" width="880" height="583"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we want to add a new element to the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt;, the first step is to check - if there is enough space for it in the array. If there is, we add item to the end of our list and the version is incremented, because the array has changed.☝️ &lt;/p&gt;

&lt;p&gt;You may ask the question, what if I want to add more than 4 elements to the array? What now, since we have no more space for new items? In this case, before adding an element, the array should be &lt;strong&gt;resized&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding an element with resize
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oDcme3_X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17zyguj0nrwl2g0l5huy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oDcme3_X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17zyguj0nrwl2g0l5huy.png" alt="Resize" width="880" height="1192"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; &lt;strong&gt;Capacity is doubled from the previous array&lt;/strong&gt;. A new array is created and &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; is modified, so version is increased by 1. In the next step, the values from the old array are copied to a new array with larger Capacity in our example equal to 8.&lt;br&gt;
After all these operations we have enough space to add a new element to the list.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing items from the List
&lt;/h2&gt;

&lt;p&gt;Another operation we can perform is to remove an element from the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt;. If we remove item with a particular index from the middle of the list, then all subsequent elements change their indexes 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wXV_7Bvg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1x7358uv6cnujki1b6yx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wXV_7Bvg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1x7358uv6cnujki1b6yx.png" alt="Remove Elements" width="880" height="695"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just like in the example, the index of element D has changed after deleting C. The size of the array decreases and the versioning mechanism remains the same. When we remove an element, as in the previous examples our version grows.&lt;/p&gt;

&lt;h2&gt;
  
  
  How AsSpan method works
&lt;/h2&gt;

&lt;p&gt;The last issue we will touch upon in the context of the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; is the &lt;strong&gt;AsSpan()&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Span&amp;lt;T&amp;gt;&lt;/code&gt; is a &lt;code&gt;ref struct&lt;/code&gt; that can be stored only on the stack. This structure contains a &lt;strong&gt;pointer&lt;/strong&gt; to a specific memory location memory and &lt;strong&gt;length&lt;/strong&gt; that describes how many elements from the memory location given span has.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uQQGfCp7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8pudo2bu1uk9ulfx361.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uQQGfCp7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8pudo2bu1uk9ulfx361.png" alt="Span" width="880" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see on the image the &lt;strong&gt;AsSpan()&lt;/strong&gt; method creates a span and sets a pointer to the first element of the array that stores values of the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt;. By wrapping the elements of the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; in &lt;code&gt;Span&amp;lt;T&amp;gt;&lt;/code&gt; structure, we can operate on a subset of data without allocating additional memory. This is a great example of how using special types that allow slicing can increase the performance of our code ✨&lt;/p&gt;

&lt;h3&gt;
  
  
  Sources
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/microsoft/referencesource/blob/master/mscorlib/system/collections/generic/list.cs"&gt;https://github.com/microsoft/referencesource/blob/master/mscorlib/system/collections/generic/list.cs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-6.0"&gt;https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-6.0&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
