Indexer Declaration in C#
📖 Scenario: Imagine you are creating a simple class to store and access the names of days in a week by their number. You want to use an indexer so that you can get or set the day name using the day number like an array.
🎯 Goal: Build a class called WeekDays that uses an indexer to get and set day names by their index (0 to 6).
📋 What You'll Learn
Create a class called
WeekDays with a private string array of length 7.Add an indexer to
WeekDays that takes an int index and returns or sets the day name at that index.Use the indexer to set the names of the days from Sunday to Saturday.
Print the day name at index 3 using the indexer.
💡 Why This Matters
🌍 Real World
Indexers let you create classes that behave like arrays or lists, making your code easier to read and use.
💼 Career
Many software jobs require creating custom data structures with indexers to simplify data access and improve code clarity.
Progress0 / 4 steps