c# – Identify classes that could be replaced by one generic class?


I’m not sure if it was your question. I’m suggesting two approaches:

  1. Interfaces
  2. Generic classes

Interfaces

public interface IVehicle {
  float Speed {get;}
  void Move(float acceleration);
}

All your classes that enherits from an interface must the interface’s methods and properties.

public class Car : IVehicle {
  private float _speed;
  float Speed {
    get => _speed;
  }
  void Move(float acceleration) {
    speed += acceleration * time;
  }
}

Generic classes

public class List<T> { }

Allows you to use any class instead of ‘T’.
Miceosoft Documentation Generic Classes

Leave a Reply

Your email address will not be published. Required fields are marked *