Example will show how to create new interface called ICarSpecifications which will hold a property and a method. ICarSpecifications interface will be implemented inside Car class and its implementation will be inside region InterfaceImplementation(note: you can manually create implementation OR when you type ICarSpecifications pres keys CTRL + . and click Implement missing members. Region will not be implemented automatically).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
interface ICarSpecifications { // void method with one parameter void CarName(string carColor); // simple auto-implemented property string CarColor { get; set; } } public class Car : ICarSpecifications { // our field private static string _carName; #region InterfaceImplementation public void CarName(string carColor) { _carName = "Black " + CarColor; } public string CarColor { get; set; } #endregion private static void Main(string[] args) { Car newCar = new Car(); newCar.CarColor = "BMW"; newCar.CarName(newCar.CarColor); Console.WriteLine("Model name is {0}", _carName); } } } |
Tomorrow we will continue with explicit interfaces implementations.