Calling the base constructor in C#

前端 未结 12 2595
南笙
南笙 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:27

    Note that you can use static methods within the call to the base constructor.

    class MyExceptionClass : Exception
    {
         public MyExceptionClass(string message, string extraInfo) : 
             base(ModifyMessage(message, extraInfo))
         {
         }
    
         private static string ModifyMessage(string message, string extraInfo)
         {
             Trace.WriteLine("message was " + message);
             return message.ToLowerInvariant() + Environment.NewLine + extraInfo;
         }
    }
    

提交回复
热议问题