Skip to content

Instantly share code, notes, and snippets.

@farukcan
Created September 29, 2022 22:29
Show Gist options
  • Select an option

  • Save farukcan/f24e790f21dedb52b87439c60a463a2c to your computer and use it in GitHub Desktop.

Select an option

Save farukcan/f24e790f21dedb52b87439c60a463a2c to your computer and use it in GitHub Desktop.
Indexer
// 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