Recap - Custom List
Part of the Object Oriented Programming section of Coddy's C# journey — lesson 40 of 70.
Challenge
EasyLet's build a CustomList class that brings together all the advanced features from this chapter - operator overloading, indexers, ToString() override, and extension methods - to create a collection that feels natural and intuitive to use.
You'll organize your code across three files:
CustomList.cs: Create aCustomListclass in theCollectionsnamespace that wraps an internal integer array and provides intuitive access patterns. Your list should have:- A private integer array to store items and a
Countproperty (with a private setter) tracking how many items are stored - A constructor that accepts a capacity (integer) and initializes the internal array to that size
- A method
Add(int item)that adds an item to the next available position (if there's room) - An indexer
this[int index]that allows getting and setting items by position - An overloaded
+operator that combines two CustomLists into a new one containing all items from both (the new list's capacity should be the sum of both counts) - An overridden
ToString()method that returns items in the format[item1, item2, item3]- only include items up toCount, not empty slots
- A private integer array to store items and a
ListExtensions.cs: Create a static classListExtensionsin theCollectionsnamespace with an extension method for your CustomList:Sum()- returns the sum of all items in the list (up toCount)
Program.cs: In your main file, create two CustomLists, add items to each, then demonstrate all the features working together - use the indexer to access items, combine lists with the+operator, print usingToString(), and calculate the sum using your extension method.
You will receive six inputs:
- Three integers to add to the first list
- Three integers to add to the second list
Create the first list with capacity 5 and the second with capacity 3. Add the three input values to each respective list.
Print the output in this format:
List 1: {ToString()}
List 2: {ToString()}
List 1[0]: {first item}
Combined: {ToString() of combined list}
Combined sum: {Sum()}For example, if the inputs are 10, 20, 30, 5, 15, and 25, the output should be:
List 1: [10, 20, 30]
List 2: [5, 15, 25]
List 1[0]: 10
Combined: [10, 20, 30, 5, 15, 25]
Combined sum: 105Notice how all the pieces work together - the indexer gives you array-like access, the + operator lets you combine lists naturally, ToString() provides readable output, and the extension method adds utility functionality. This is what well-designed classes feel like!
Try it yourself
using System;
using Collections;
class Program
{
public static void Main(string[] args)
{
// Read six integers
int a1 = Convert.ToInt32(Console.ReadLine());
int a2 = Convert.ToInt32(Console.ReadLine());
int a3 = Convert.ToInt32(Console.ReadLine());
int b1 = Convert.ToInt32(Console.ReadLine());
int b2 = Convert.ToInt32(Console.ReadLine());
int b3 = Convert.ToInt32(Console.ReadLine());
// TODO: Create first CustomList with capacity 5
// TODO: Add a1, a2, a3 to the first list
// TODO: Create second CustomList with capacity 3
// TODO: Add b1, b2, b3 to the second list
// TODO: Print List 1 using ToString()
// TODO: Print List 2 using ToString()
// TODO: Print List 1[0] using the indexer
// TODO: Combine lists using + operator and print the result
// TODO: Print the sum of the combined list using the Sum() extension method
}
}
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesNamespaces & DirectivesIntro to Classes & ObjectsThe 'this' KeywordMethods and ParametersFields vs PropertiesConstructorsObject InitializersRecap - Simple Calculator4Inheritance
Basic Inheritance (:) SyntaxThe 'base' KeywordVirtual & Override KeywordsSealed ClassesThe 'object' Base ClassRecap - Employee Hierarchy7Advanced Features
Operator OverloadingIndexers (this[])ToString() OverrideExtension MethodsRecap - Custom List2Properties & Static Members
Auto-Implemented PropertiesRead/Write-Only PropertiesStatic Fields & MethodsStatic ClassesExpression-Bodied Members5Polymorphism & Interfaces
Compile vs Runtime PolyInterface vs Abstract ClassMultiple InterfacesExplicit InterfacesUpcasting & DowncastingRecap - Shape Calculator