Collections are fundamental to managing and organizing data in C#. They provide efficient ways to store, retrieve, and manipulate groups of objects. This lesson focuses on implementing a program that uses collections to manage a list of products, building upon the foundational knowledge of arrays, lists, and dictionaries covered in the previous lessons. By the end of this lesson, you'll be able to create, populate, and manipulate collections to solve practical data management problems.
The task is to create a program that manages a list of products using C# collections. This involves defining a Product class, creating a collection to store multiple Product objects, and implementing functionalities to add, remove, search, and display products. This exercise will reinforce your understanding of how to use collections effectively in a real-world scenario.
Product ClassFirst, you need to define a Product class that represents a product with properties like name, price, and category. This class will serve as the data structure for each item in your collection.
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public Product(string name, decimal price, string category)
{
Name = name;
Price = price;
Category = category;
}
public override string ToString()
{
return $"Name: {Name}, Price: {Price}, Category: {Category}";
}
}
Explanation:
public class Product: Defines a class named Product.public string Name { get; set; }: Defines a public property Name of type string to store the product's name. The { get; set; } syntax allows both reading and writing to this property.public decimal Price { get; set; }: Defines a public property Price of type decimal to store the product's price. Using decimal is recommended for monetary values to avoid floating-point precision issues.public string Category { get; set; }: Defines a public property Category of type string to store the product's category.public Product(string name, decimal price, string category): Defines a constructor for the Product class. A constructor is a special method that is called when an object of the class is created. It initializes the object's properties with the provided values.public override string ToString(): Overrides the ToString method to provide a custom string representation of the Product object. This is useful for displaying product information in a user-friendly format.Based on the requirements, a List<Product> is a suitable choice for storing the products. A List is a dynamic array that can grow or shrink as needed, making it flexible for managing a list of products where the number of products may change.
using System.Collections.Generic; // Import the namespace for List<T>
public class ProductManager
{
private List<Product> products = new List<Product>();
// Methods to add, remove, search, and display products will be added here
}
Explanation:
using System.Collections.Generic;: Imports the System.Collections.Generic namespace, which contains the List<T> class.private List<Product> products = new List<Product>();: Declares a private field named products of type List<Product>. This list will store the Product objects. The new List<Product>() part creates a new, empty list. The private keyword ensures that this list can only be accessed from within the ProductManager class.