From the course: C# Hands-on Practice with Data-Structures
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
GitHub Codespaces Solution: Shopping cart - C# Tutorial
From the course: C# Hands-on Practice with Data-Structures
GitHub Codespaces Solution: Shopping cart
Let's implement the methods of the CartService to create a functional shopping cart application. To start off, we'll create a list to keep track of the items in our cart. We make this read-only so that the reference to the cart cannot be changed after it's initialized. This means that after the list is created, we cannot reassign the cart to point to a different list or null. However, we can modify the contents of the list itself, like adding or removing CartItem objects. This is because read-only only applies to the reference and not the internal state of the object. For the GetAll method, we'll return the cart itself. This works because the List class implements the IEnumerable interface, which provides functionality for iterating over a collection of items. To add a product to the cart, we'll first check if the product is already in the cart. If it is, we'll want to update its quantity and increase it by one. The FirstOrDefault method returns the first item that matches with the…