Created
September 29, 2022 22:29
-
-
Save farukcan/f24e790f21dedb52b87439c60a463a2c to your computer and use it in GitHub Desktop.
Indexer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Vehicle vehicle = new Vehicle("Car"); | |
| // vehicle["frame"] = "carbon"; | |
| // vehicle["wheelCount"] = "4"; | |
| class Vehicle | |
| { | |
| private string _vehicleType; | |
| private Dictionary<string, string> _parts = | |
| new Dictionary<string, string>(); | |
| // Constructor | |
| public Vehicle(string vehicleType) | |
| { | |
| this._vehicleType = vehicleType; | |
| } | |
| // Indexer | |
| public string this[string key] | |
| { | |
| get { return _parts[key]; } | |
| set { _parts[key] = value; } | |
| } | |
| public void Show() | |
| { | |
| Console.WriteLine("\n---------------------------"); | |
| Console.WriteLine("Vehicle Type: {0}", _vehicleType); | |
| Console.WriteLine(" Frame : {0}", _parts["frame"]); | |
| Console.WriteLine(" Engine : {0}", _parts["engine"]); | |
| Console.WriteLine(" #Wheels: {0}", _parts["wheels"]); | |
| Console.WriteLine(" #Doors : {0}", _parts["doors"]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment