{"id":116611,"date":"2024-05-08T07:45:42","date_gmt":"2024-05-08T05:45:42","guid":{"rendered":"https:\/\/code-maze.com\/?p=116611"},"modified":"2024-05-08T07:45:42","modified_gmt":"2024-05-08T05:45:42","slug":"csharp-update-list-item","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-update-list-item\/","title":{"rendered":"How to Update an Item in a List in C#"},"content":{"rendered":"<p>In this article, we&#8217;ll look at how to update an item in a List in C#, using a simple library management system as an example. We&#8217;ll explore the intricacies of this common operation and learn how to manipulate lists in our C# applications effectively.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/collections-csharp\/UpdateItemInListCSharp\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s start.<\/p>\n<h2>Understand List in C#<\/h2>\n<p>In the world of C#, one of the most commonly used data structures is <code>List&lt;T&gt;<\/code>. This versatile <strong>collection class provides the ability to store and manipulate a series of elements of a specific type<\/strong>, labeled <code>T<\/code>. It&#8217;s similar to an array but with more features. It dynamically adjusts its size as we add or remove elements, and it has several useful methods for sorting, searching, and editing data.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To gain a better understanding of the <code>List<\/code> class, please check out our article <a href=\"https:\/\/code-maze.com\/csharp-generic-list-dictionary\/\" target=\"_blank\" rel=\"noopener\">C# Intermediate \u2013 Generic List and Dictionary<\/a>.<\/div>\n<h2>List Set up for Finding and Updating Item<\/h2>\n<p>There are several methods for finding elements in a <code>List<\/code>, each with its own strengths and ideal use cases. We will set up a small library program and go through the different methods.<\/p>\n<p>Let&#8217;s start creating a book model:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Book(string title, string author, string isbn, bool isCheckedOut)\r\n{\r\n    public string Title { get; set; } = title;\r\n\r\n    public string Author { get; set; } = author;\r\n\r\n    public string ISBN { get; set; } = isbn;\r\n\r\n    public bool IsCheckedOut { get; set; } = isCheckedOut;\r\n}<\/pre>\n<p>Let&#8217;s continue creating the library class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Library\r\n{\r\n    public List&lt;Book&gt; Books { get; set; }\r\n\r\n    public Library()\r\n    {\r\n        Books =\r\n        [\r\n            new(\"Harry Potter and the Philosopher's Stone\", \"J.K. Rowling\", \"978-0439708180\", false),\r\n            new(\"Harry Potter and the Chamber of Secrets\", \"J.K. Rowling\", \"978-0439064873\", false),\r\n            new(\"Harry Potter and the Goblet of Fire\", \"J.K. Rowling\", \"978-0439139601\", false),\r\n            new(\"Harry Potter and the Order of the Phoenix\", \"J.K. Rowling\", \"978-0439358071\", false),\r\n            new(\"Harry Potter and the Half-Blood Prince\", \"J.K. Rowling\", \"978-0439785969\", false),\r\n        ];\r\n    }\r\n\r\n    public Book AddBook(string title, string author, string isbn, bool isCheckedOut)\r\n    {\r\n        var book = new Book(title, author,\u00a0isbn, isCheckedOut);\r\n        Books.Add(book);\r\n\r\n        return book;\r\n    }\r\n}<\/pre>\n<p>In our Library class, there are two main parts. First, our constructor\u00a0seeds the publicly accessible list <code>Books<\/code> with 5 newly created book objects. Secondly, our <code>AddBook()<\/code> method creates a new book object using the specified parameter values for the author, title,\u00a0 ISBN, and checkout status. The newly created book is then added to our <code>Books<\/code> list.<\/p>\n<h2><span class=\"TextRun SCXW105888573 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW105888573 BCX8\" data-ccp-parastyle=\"heading 2\">Find and Update an Item in a List Using Built-In Methods<\/span><\/span><\/h2>\n<p>By default, C# offers methods to update items in a list. Let&#8217;s have a look at these methods.<\/p>\n<h3>Update List Item With IndexOf() Method<\/h3>\n<p>The <code>IndexOf()<\/code> method is simple and efficient. <strong>It returns the zero-based index of the first occurrence of an element in the list.<\/strong> However, it uses the standard equality comparator, which we have to override to match our book comparison case.<\/p>\n<p>Let&#8217;s use an index first:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public void CheckoutBookUsingIndexOf(Book book)\r\n{\r\n    var index = Books.IndexOf(book);\r\n    if (index &gt; -1)\r\n    {\r\n        Books[index].IsCheckedOut = true;\r\n    }\r\n}<\/pre>\n<p>The <code>IndexOf()<\/code> method we call in our <code>CheckoutBookUsingIndexOf()<\/code> method, uses our provided book object to check the <code>Books<\/code> list from our <code>Library<\/code> class. In return, we receive the index of the <code>book<\/code> object in the <code>Books<\/code> list. Then, we check whether the index is in a valid range and specify the object via the index of the <code>book<\/code> object in the <code>Books<\/code> list and change its boolean field <code>IsCheckedOut<\/code> to <code>true<\/code>.<\/p>\n<p>As already announced, we still have to make changes to our book class for the <code>IndexOf()<\/code> method so that we get the right book back. <strong>Internally, the <code>IndexOf()<\/code> method calls the equality comparator Equals() to compare the books.<\/strong> By default, <code>Equals()<\/code> checks reference equality for reference types such as classes, i.e. it checks whether two object references point to the same instance in memory, not whether their content is the same.<\/p>\n<p>In our case, two <code>Book<\/code> objects are not considered the same if they are the same object in memory, but if their <code>Title<\/code>, <code>Author<\/code> and <code>ISBN<\/code> properties are the same (essentially two <code>Book<\/code> objects representing the same actual book).<\/p>\n<p>So let&#8217;s update our Book class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"11-21\">public class Book(string title, string author, string isbn, bool isCheckedOut)\r\n{\r\n    public string Title { get; set; } = title;\r\n\r\n    public string Author { get; set; } = author;\r\n\r\n    public string ISBN { get; set; } = isbn;\r\n\r\n    public bool IsCheckedOut { get; set; } = isCheckedOut;\r\n\r\n    public override bool Equals(object? obj)\r\n    {\r\n        if (obj is Book b)\r\n        {\r\n            return this.Title == b.Title &amp;&amp; this.Author == b.Author &amp;&amp; this.ISBN == b.ISBN;\r\n        }\r\n        else\r\n        {\r\n            return false;\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong>Here, we override the <code>Equals()<\/code> method to change its behavior so that it compares objects based on their properties (title, author, and ISBN) rather than their reference.<\/strong> This way, when <code>IndexOf()<\/code> calls <code>Equals()<\/code> internally, it behaves as we intend and considers two <code>Book<\/code> objects with identical <code>Title<\/code>, <code>Author<\/code> and <code>ISBN<\/code> to be the same, even if they are different instances.<\/p>\n<h3>Use the Find() Method to Find And Update a List Item<\/h3>\n<p>The <code>Find()<\/code> method is more flexible. <strong>It takes a predicate (a function that returns true or false) and returns the first element that fulfills the condition<\/strong>. This is useful when we need to find an element based on a complex condition.<\/p>\n<p>Let&#8217;s create a checkout method using the <code>Find()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public void CheckoutBookUsingFind(string isbn)\r\n{\r\n    var bookToCheckout = Books.Find(b =&gt; b.ISBN == isbn);\r\n    if (bookToCheckout is not null)\r\n    {\r\n        bookToCheckout.IsCheckedOut = true;\r\n    }\r\n}<\/pre>\n<p>In our <code>CheckoutBookUsingFind()<\/code> method we call the <code>Find()<\/code> method on our <code>Books<\/code> list. As a parameter, the <code>Find()<\/code> method accepts a lambda expression which returns us a book <code>b<\/code> and we check whether the ISBN of <code>b<\/code> is equal to <code>isbn<\/code>.\u00a0 If not, the return value is <code>null<\/code>. We store the return value in a new <code>Book<\/code> object <code>bookToCheckout<\/code> and check if <code>bookToCheckout<\/code> is null. If the <code>Find()<\/code> method returns an object, we then change the <code>IsCheckedOut<\/code> property of it to <code>true<\/code>.<\/p>\n<h3>List Item Update Using FindIndex() Method<\/h3>\n<p>Similarly, <code>FindIndex()<\/code> <strong>returns the index of the first element that fulfills a specific predicate<\/strong>. This is useful when we need the position of an element in the list, not just the element itself.<\/p>\n<p>Let&#8217;s create a method similar to our previous examples, but now using the <code>FindIndex()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public void CheckoutBookUsingFindIndex(string isbn)\r\n{\r\n    var index = Books.FindIndex(b =&gt; b.ISBN == isbn);\r\n    if (index != -1)\r\n    {\r\n        Books[index].IsCheckedOut = true;\r\n    }\r\n}<\/pre>\n<p>In our <code>CheckoutBookUsingFindIndex()<\/code> method, we call the <code>FindIndex()<\/code> method on the <code>Books<\/code> list. We use the same condition as we used in our previously created <code>CheckoutBookUsingFind()<\/code> method. Instead of returning the matching object for the condition, the <code>FindIndex()<\/code> method returns the index of the matching object in our <code>Books<\/code> list. We then check the return value. If it is not negative, we check out the book using the index of it on our <code>Books<\/code> list.<\/p>\n<h2><span class=\"TextRun SCXW123758649 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW123758649 BCX8\" data-ccp-parastyle=\"heading 2\">Use LINQ to Find and Update <\/span><\/span><span class=\"TextRun SCXW105888573 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW105888573 BCX8\" data-ccp-parastyle=\"heading 2\">a List Item<\/span><\/span><\/h2>\n<p>In C#, we can also use the <a href=\"https:\/\/code-maze.com\/linq-csharp-basic-concepts\/\" target=\"_blank\" rel=\"noopener\">Language Integrated Query (LINQ)<\/a> to find and update an element in a list. This approach is more flexible and more efficient than conventional methods.<\/p>\n<h3>Use LINQ FirstOrDefault() Method<\/h3>\n<p><code>LINQ<\/code> methods such as <code>FirstOrDefault()<\/code> offer even more flexibility. <strong>This method returns the first element that fulfills a condition, or the default value for the type if no such element is found<\/strong>. This is a safe option if we are unsure whether an element is present in the list:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public void CheckoutBookUsingFirstOrDefault(string isbn)\r\n{\r\n    var bookToCheckout = Books.FirstOrDefault(b =&gt; b.ISBN == isbn);\r\n    if (bookToCheckout is not null)\r\n    {\r\n        bookToCheckout.IsCheckedOut = true;\r\n    }\r\n}<\/pre>\n<h3>SingleOrDefault() Method To Update Item<\/h3>\n<p>The <code>SingleOrDefault()<\/code> method works similarly to the <code>FirstOrDefault()<\/code> method but is a bit stricter. <strong>If an item matches the given condition the item does get returned. But unlike with<\/strong> <code>FirstOrDefault()<\/code> <strong>method, an exception is thrown if more than one item matches the condition:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public void CheckoutBookUsingSingleOrDefault(string isbn)\r\n{\r\n    var bookToCheckout = Books.SingleOrDefault(b =&gt; b.ISBN == isbn);\r\n    if (bookToCheckout is not null)\r\n    {\r\n        bookToCheckout.IsCheckedOut = true;\r\n    }\r\n}<\/pre>\n<h2><span class=\"TextRun SCXW247991456 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW247991456 BCX8\" data-ccp-parastyle=\"heading 2\">Find and Update <\/span><\/span><span class=\"TextRun SCXW105888573 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW105888573 BCX8\" data-ccp-parastyle=\"heading 2\">an Item in a List <\/span><\/span><span class=\"TextRun SCXW247991456 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW247991456 BCX8\" data-ccp-parastyle=\"heading 2\">Using ForEach Loop<\/span><\/span><\/h2>\n<p>The most basic approach is to iterate the Books list using a foreach loop:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public void CheckoutBookUsingForeach(string isbn)\r\n{\r\n    foreach (var book in Books)\r\n    {\r\n        if (book.ISBN == isbn)\r\n        {\r\n            book.IsCheckedOut = true;\r\n        }\r\n    }\r\n}<\/pre>\n<h2><span class=\"TextRun SCXW73595538 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW73595538 BCX8\" data-ccp-parastyle=\"heading 2\">Performance Comparision of Find and Update List Item Methods<\/span><\/span><\/h2>\n<p>Now that we have learned about different methods for updating an element in a list, let&#8217;s evaluate which method is the most performant. For performance benchmarking and comparison, we use the <a href=\"https:\/\/code-maze.com\/benchmarking-csharp-and-asp-net-core-projects\/\" target=\"_blank\" rel=\"noopener\">BechmarkDotNet<\/a> framework. We call each created method and have the first entry from our books list returned.<\/p>\n<p>Let&#8217;s take a look at the results:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">| Method                           | Mean     | Error    | StdDev   | Median   | Rank |\r\n|--------------------------------- |---------:|---------:|---------:|---------:|-----:|\r\n| CheckoutBookUsingFind            | 17.29 ns | 0.699 ns | 1.949 ns | 16.79 ns |    1 |\r\n| CheckoutBookUsingFindIndex       | 18.07 ns | 1.069 ns | 3.085 ns | 17.30 ns |    1 |\r\n| CheckoutBookUsingForeach         | 21.09 ns | 0.449 ns | 0.518 ns | 20.99 ns |    2 |\r\n| CheckoutBookUsingIndexOf         | 23.16 ns | 0.473 ns | 0.545 ns | 23.06 ns |    3 |\r\n| CheckoutBookUsingFirstOrDefault  | 35.04 ns | 1.964 ns | 5.760 ns | 32.27 ns |    4 |\r\n| CheckoutBookUsingSingleOrDefault | 72.29 ns | 1.435 ns | 1.199 ns | 72.45 ns |    5 |<\/pre>\n<p>Here, we see the average time required by the various methods to update an element in a list. The <code>CheckoutBookUsingFind()<\/code> and the\u00a0<code>CheckoutBookUsingFindIndex()<\/code> methods have the shortest average time and are, therefore, the most efficient, followed by the <code>CheckoutBookUsingForeach()<\/code> method. The least efficient methods are <code>CheckoutBookUsingFirstOrDefault()<\/code> and <code>CheckoutBookUsingSingleOrDefault()<\/code> with comparatively longer mean times.<\/p>\n<h2><span class=\"TextRun SCXW139467675 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW139467675 BCX8\" data-ccp-parastyle=\"heading 2\">Conclusion<\/span><\/span><\/h2>\n<p>In conclusion, the method to use for updating an item in a List in C# depends heavily on the specific project requirements and environment. Our performance test shows that while CheckoutBookUsingFind() is the most efficient, other methods offer unique advantages. CheckoutBookUsingFirstOrDefault() avoids exceptions when nothing is found, and CheckoutBookUsingForeach() is suitable for processing any element. A deep understanding of the individual methods can therefore help us to design our applications more effectively. The &#8220;best&#8221; method strikes a balance between project requirements, desired results, and performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;ll look at how to update an item in a List in C#, using a simple library management system as an example. We&#8217;ll explore the intricacies of this common operation and learn how to manipulate lists in our C# applications effectively. Let&#8217;s start. Understand List in C# In the world of C#, [&hellip;]<\/p>\n","protected":false},"author":151,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[1811,2140,2088,1588],"class_list":["post-116611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-c","tag-c-lists","tag-csharp","tag-lists","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Update an Item in a List in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we will discuss how to update an item in a List in C# with a simple example of each approach.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-update-list-item\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Update an Item in a List in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we will discuss how to update an item in a List in C# with a simple example of each approach.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-update-list-item\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2024-05-08T05:45:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Kai Westerschwiensterdt\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kai Westerschwiensterdt\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-update-list-item\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-update-list-item\/\"},\"author\":{\"name\":\"Kai Westerschwiensterdt\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/c79f96cf8f6f6347bacfc39f76c7c47d\"},\"headline\":\"How to Update an Item in a List in C#\",\"datePublished\":\"2024-05-08T05:45:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-update-list-item\/\"},\"wordCount\":1189,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-update-list-item\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"C#\",\"c# lists\",\"csharp\",\"lists\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-update-list-item\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-update-list-item\/\",\"url\":\"https:\/\/code-maze.com\/csharp-update-list-item\/\",\"name\":\"How to Update an Item in a List in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-update-list-item\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-update-list-item\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2024-05-08T05:45:42+00:00\",\"description\":\"In this article, we will discuss how to update an item in a List in C# with a simple example of each approach.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-update-list-item\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-update-list-item\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-update-list-item\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-update-list-item\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Update an Item in a List in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/c79f96cf8f6f6347bacfc39f76c7c47d\",\"name\":\"Kai Westerschwiensterdt\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/064537f28f94feb4f8a33b6e0476da96?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/064537f28f94feb4f8a33b6e0476da96?s=96&d=mm&r=g\",\"caption\":\"Kai Westerschwiensterdt\"},\"description\":\"Hello! I'm Kai Westerschwiensterdt, a software developer with expertise in .NET, AWS, Git, and Azure DevOps. My professional journey began in 2021 after three years of training. When not absorbed in coding or learning the ins and outs of AWS, I enjoy handball and making lasting friendships. Welcome to my slice of the digital world!\",\"sameAs\":[\"https:\/\/de.linkedin.com\/in\/kai-westerschwiensterdt-24437622a\"],\"url\":\"https:\/\/code-maze.com\/author\/kai-westerschwiensterdt\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Update an Item in a List in C# - Code Maze","description":"In this article, we will discuss how to update an item in a List in C# with a simple example of each approach.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-update-list-item\/","og_locale":"en_US","og_type":"article","og_title":"How to Update an Item in a List in C# - Code Maze","og_description":"In this article, we will discuss how to update an item in a List in C# with a simple example of each approach.","og_url":"https:\/\/code-maze.com\/csharp-update-list-item\/","og_site_name":"Code Maze","article_published_time":"2024-05-08T05:45:42+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Kai Westerschwiensterdt","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Kai Westerschwiensterdt","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-update-list-item\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-update-list-item\/"},"author":{"name":"Kai Westerschwiensterdt","@id":"https:\/\/code-maze.com\/#\/schema\/person\/c79f96cf8f6f6347bacfc39f76c7c47d"},"headline":"How to Update an Item in a List in C#","datePublished":"2024-05-08T05:45:42+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-update-list-item\/"},"wordCount":1189,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-update-list-item\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["C#","c# lists","csharp","lists"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-update-list-item\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-update-list-item\/","url":"https:\/\/code-maze.com\/csharp-update-list-item\/","name":"How to Update an Item in a List in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-update-list-item\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-update-list-item\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2024-05-08T05:45:42+00:00","description":"In this article, we will discuss how to update an item in a List in C# with a simple example of each approach.","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-update-list-item\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-update-list-item\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-update-list-item\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-update-list-item\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"How to Update an Item in a List in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/c79f96cf8f6f6347bacfc39f76c7c47d","name":"Kai Westerschwiensterdt","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/064537f28f94feb4f8a33b6e0476da96?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/064537f28f94feb4f8a33b6e0476da96?s=96&d=mm&r=g","caption":"Kai Westerschwiensterdt"},"description":"Hello! I'm Kai Westerschwiensterdt, a software developer with expertise in .NET, AWS, Git, and Azure DevOps. My professional journey began in 2021 after three years of training. When not absorbed in coding or learning the ins and outs of AWS, I enjoy handball and making lasting friendships. Welcome to my slice of the digital world!","sameAs":["https:\/\/de.linkedin.com\/in\/kai-westerschwiensterdt-24437622a"],"url":"https:\/\/code-maze.com\/author\/kai-westerschwiensterdt\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/116611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/151"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=116611"}],"version-history":[{"count":2,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/116611\/revisions"}],"predecessor-version":[{"id":116616,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/116611\/revisions\/116616"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=116611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=116611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=116611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}