From b7bec4eaa2499314b06b7f6986def38c285db50c Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 29 Nov 2026 20:01:06 +0100 Subject: [PATCH] Update UpdateItemInListCSharp to .NET 11, add ReplaceBook and fix the benchmark Both projects move to net11.0 with BenchmarkDotNet 0.15.8, xunit 2.9.3, xunit.runner.visualstudio 4.0.1, Microsoft.NET.Test.Sdk 18.12.0 and coverlet.collector 10.1.1. The benchmark runs in process, every method searches the same first book and returns a result. Library gains ReplaceBook with a test, and Book lists its properties in article order. --- .../LibraryTest.cs | 14 +++++++++++++ .../UpdateItemInListCSharp.Test.csproj | 10 ++++----- .../UpdateItemInListCSharp/BenchmarkTester.cs | 21 ++++++++++++------- .../UpdateItemInListCSharp/Library.cs | 9 ++++++++ .../UpdateItemInListCSharp/Models/Book.cs | 4 ++-- .../UpdateItemInListCSharp.csproj | 4 ++-- 6 files changed, 46 insertions(+), 16 deletions(-) diff --git a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp.Test/LibraryTest.cs b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp.Test/LibraryTest.cs index 4f2e417f59..69b10df8be 100644 --- a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp.Test/LibraryTest.cs +++ b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp.Test/LibraryTest.cs @@ -89,4 +89,18 @@ public void WhenCheckoutBookSingleOrDefault_ThenBookIsCheckedOut() // Assert Assert.True(book.IsCheckedOut); } + + [Fact] + public void WhenReplaceBook_ThenListHoldsNewBook() + { + // Arrange + var newBook = new Book("Harry Potter and the Philosopher's Stone", "J.K. Rowling", "978-0439708180", true); + + // Act + _library.ReplaceBook("978-0439708180", newBook); + + // Assert + Assert.Same(newBook, _library.Books[0]); + Assert.Equal(5, _library.Books.Count); + } } diff --git a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp.Test/UpdateItemInListCSharp.Test.csproj b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp.Test/UpdateItemInListCSharp.Test.csproj index 6e1d80c488..6bae702652 100644 --- a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp.Test/UpdateItemInListCSharp.Test.csproj +++ b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp.Test/UpdateItemInListCSharp.Test.csproj @@ -1,7 +1,7 @@ - net8.0 + net11.0 enable enable @@ -10,13 +10,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/BenchmarkTester.cs b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/BenchmarkTester.cs index 69f9b82190..b5b4a8a363 100644 --- a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/BenchmarkTester.cs +++ b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/BenchmarkTester.cs @@ -3,6 +3,7 @@ namespace UpdateItemInListCSharp; +[InProcess] [MemoryDiagnoser(true)] [Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)] [RankColumn] @@ -15,43 +16,49 @@ public class BenchmarkTester public void GlobalSetup() { _library = new Library(); - var book = _library.Books.Last(); + var book = _library.Books.First(); _newBook = new Book(book.Title, book.Author, book.ISBN, book.IsCheckedOut); } [Benchmark] - public void CheckoutBookUsingFind() + public bool CheckoutBookUsingFind() { _library.CheckoutBookUsingFind("978-0439708180"); + return _library.Books[0].IsCheckedOut; } [Benchmark] - public void CheckoutBookUsingFindIndex() + public bool CheckoutBookUsingFindIndex() { _library.CheckoutBookUsingFindIndex("978-0439708180"); + return _library.Books[0].IsCheckedOut; } [Benchmark] - public void CheckoutBookUsingFirstOrDefault() + public bool CheckoutBookUsingFirstOrDefault() { _library.CheckoutBookUsingFirstOrDefault("978-0439708180"); + return _library.Books[0].IsCheckedOut; } [Benchmark] - public void CheckoutBookUsingForeach() + public bool CheckoutBookUsingForeach() { _library.CheckoutBookUsingForeach("978-0439708180"); + return _library.Books[0].IsCheckedOut; } [Benchmark] - public void CheckoutBookUsingIndexOf() + public bool CheckoutBookUsingIndexOf() { _library.CheckoutBookUsingIndexOf(_newBook); + return _library.Books[0].IsCheckedOut; } [Benchmark] - public void CheckoutBookUsingSingleOrDefault() + public bool CheckoutBookUsingSingleOrDefault() { _library.CheckoutBookUsingSingleOrDefault("978-0439708180"); + return _library.Books[0].IsCheckedOut; } } diff --git a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/Library.cs b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/Library.cs index e83d063dc6..7e09e8b490 100644 --- a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/Library.cs +++ b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/Library.cs @@ -74,6 +74,15 @@ public void CheckoutBookUsingIndexOf(Book book) } } + public void ReplaceBook(string isbn, Book newBook) + { + var index = Books.FindIndex(b => b.ISBN == isbn); + if (index != -1) + { + Books[index] = newBook; + } + } + public void CheckoutBookUsingSingleOrDefault(string isbn) { var bookToCheckout = Books.SingleOrDefault(b => b.ISBN == isbn); diff --git a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/Models/Book.cs b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/Models/Book.cs index 0d176168ac..f8d2f3c43e 100644 --- a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/Models/Book.cs +++ b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/Models/Book.cs @@ -2,14 +2,14 @@ public class Book(string title, string author, string isbn, bool isCheckedOut) { + public string Title { get; set; } = title; + public string Author { get; set; } = author; public string ISBN { get; set; } = isbn; public bool IsCheckedOut { get; set; } = isCheckedOut; - public string Title { get; set; } = title; - public override bool Equals(object? obj) { if (obj is Book b) diff --git a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/UpdateItemInListCSharp.csproj b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/UpdateItemInListCSharp.csproj index 848f901f4f..32068aef71 100644 --- a/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/UpdateItemInListCSharp.csproj +++ b/collections-csharp/UpdateItemInListCSharp/UpdateItemInListCSharp/UpdateItemInListCSharp.csproj @@ -2,13 +2,13 @@ Exe - net8.0 + net11.0 enable enable - +