Calling the base constructor in C#

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

    As per some of the other answers listed here, you can pass parameters into the base class constructor. It is advised to call your base class constructor at the beginning of the constructor for your inherited class.

    public class MyException : Exception
    {
        public MyException(string message, string extraInfo) : base(message)
        {
        }
    }
    

    I note that in your example you never made use of the extraInfo parameter, so I assumed you might want to concatenate the extraInfo string parameter to the Message property of your exception (it seems that this is being ignored in the accepted answer and the code in your question).

    This is simply achieved by invoking the base class constructor, and then updating the Message property with the extra info.

    public class MyException: Exception
    {
        public MyException(string message, string extraInfo) : base($"{message} Extra info: {extraInfo}")
        {
        }
    }
    
    0 讨论(0)
  • 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;
         }
    }
    
    0 讨论(0)
  • 2020-11-22 03:28
    public class MyExceptionClass : Exception
    {
        public MyExceptionClass(string message,
          Exception innerException): base(message, innerException)
        {
            //other stuff here
        }
    }
    

    You can pass inner exception to one of the constructors.

    0 讨论(0)
  • 2020-11-22 03:29
    public class MyException : Exception
    {
        public MyException() { }
        public MyException(string msg) : base(msg) { }
        public MyException(string msg, Exception inner) : base(msg, inner) { }
    }
    
    0 讨论(0)
  • 2020-11-22 03:31

    Using newer C# features, namely out var, you can get rid of the static factory-method. I just found out (by accident) that out var parameter of methods called inse base-"call" flow to the constructor body.

    Example, using this base class you want to derive from:

    public abstract class BaseClass
    {
        protected BaseClass(int a, int b, int c)
        {
        }
    }
    

    The non-compiling pseudo code you want to execute:

    public class DerivedClass : BaseClass
    {
        private readonly object fatData;
    
        public DerivedClass(int m)
        {
            var fd = new { A = 1 * m, B = 2 * m, C = 3 * m };
            base(fd.A, fd.B, fd.C); // base-constructor call
            this.fatData = fd;
        }
    }
    

    And the solution by using a static private helper method which produces all required base arguments (plus additional data if needed) and without using a static factory method, just plain constructor to the outside:

    public class DerivedClass : BaseClass
    {
        private readonly object fatData;
    
        public DerivedClass(int m)
            : base(PrepareBaseParameters(m, out var b, out var c, out var fatData), b, c)
        {
            this.fatData = fatData;
            Console.WriteLine(new { b, c, fatData }.ToString());
        }
    
        private static int PrepareBaseParameters(int m, out int b, out int c, out object fatData)
        {
            var fd = new { A = 1 * m, B = 2 * m, C = 3 * m };
            (b, c, fatData) = (fd.B, fd.C, fd); // Tuples not required but nice to use
            return fd.A;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:32

    It is true use the base (something) to call the base class constructor, but in case of overloading use the this keyword

    public ClassName() : this(par1,par2)
    {
    // do not call the constructor it is called in the this.
    // the base key- word is used to call a inherited constructor   
    } 
    
    // Hint used overload as often as needed do not write the same code 2 or more times
    
    0 讨论(0)
提交回复
热议问题