Calling the base constructor in C#

前端 未结 12 2774
南笙
南笙 2020-11-22 03:04

If I inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how do I do that?

For exa

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 03:06

    public class Car
    {
         public Car(string model)
         {
            Console.WriteLine(model);
         }
    }
    
    public class Mercedes : Car
    {
         public Mercedes(string model): base(model)
         {
    
         }
    }
    

    Usage:

    Mercedes mercedes = new Mercedes("CLA Shooting Brake");
    

    Output: CLA Shooting Brake

提交回复
热议问题