Calling the base constructor in C#

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

    Modify your constructor to the following so that it calls the base class constructor properly:

    public class MyExceptionClass : Exception
    {
        public MyExceptionClass(string message, string extrainfo) : base(message)
        {
            //other stuff here
        }
    }
    

    Note that a constructor is not something that you can call anytime within a method. That's the reason you're getting errors in your call in the constructor body.

提交回复
热议问题