0
0
C Sharp (C#)programming~30 mins

Multi-parameter indexers in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Multi-parameter Indexers in C#
📖 Scenario: Imagine you manage a small hotel with rooms on different floors. You want to keep track of which guest is staying in each room using a simple system.
🎯 Goal: You will create a class with a multi-parameter indexer that lets you get and set guest names by specifying the floor and room number.
📋 What You'll Learn
Create a class called Hotel that stores guest names.
Use a multi-parameter indexer with two int parameters: floor and room.
The indexer should allow getting and setting guest names by floor and room.
Demonstrate setting and getting guest names using the indexer.
💡 Why This Matters
🌍 Real World
Multi-parameter indexers are useful when you want to access data stored in a grid or table-like structure, such as seats in a theater, cells in a spreadsheet, or rooms in a hotel.
💼 Career
Understanding multi-parameter indexers helps you write cleaner and more intuitive code when working with complex data structures, a common task in software development jobs.
Progress0 / 4 steps
1
Create the Hotel class with a 2D array
Create a class called Hotel with a private 2D string array called guests sized 3 floors by 5 rooms each. Initialize the array in the constructor.
C Sharp (C#)
Need a hint?

Use a 2D array to store guest names by floor and room.

2
Add a multi-parameter indexer to the Hotel class
Inside the Hotel class, add a public indexer with two int parameters named floor and room. The indexer should get and set values in the guests array.
C Sharp (C#)
Need a hint?

Use this[int floor, int room] to define the indexer with get and set accessors.

3
Create a Hotel object and assign guest names
In the Main method, create a Hotel object called myHotel. Use the indexer to assign "Alice" to floor 0, room 1 and "Bob" to floor 2, room 4.
C Sharp (C#)
Need a hint?

Create the object and use the indexer with two numbers inside square brackets to assign names.

4
Print guest names using the multi-parameter indexer
In the Main method, add two Console.WriteLine statements to print the guests at floor 0, room 1 and floor 2, room 4 using the indexer on myHotel.
C Sharp (C#)
Need a hint?

Use Console.WriteLine(myHotel[0, 1]) and Console.WriteLine(myHotel[2, 4]) to print the guest names.