Calling the base constructor in C#

前端 未结 12 2592
南笙
南笙 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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 03:09
    class Exception
    {
         public Exception(string message)
         {
             [...]
         }
    }
    
    class MyExceptionClass : Exception
    {
         public MyExceptionClass(string message, string extraInfo)
         : base(message)
         {
             [...]
         }
    }
    
    0 讨论(0)
  • 2020-11-22 03:11

    From Framework Design Guidelines and FxCop rules.:

    1. Custom Exception should have a name that ends with Exception

        class MyException : Exception
    

    2. Exception should be public

        public class MyException : Exception
    

    3. CA1032: Exception should implements standard constructors.

    • A public parameterless constructor.
    • A public constructor with one string argument.
    • A public constructor with one string and Exception (as it can wrap another Exception).
    • A serialization constructor protected if the type is not sealed and private if the type is sealed. Based on MSDN:

      [Serializable()]
      public class MyException : Exception
      {
        public MyException()
        {
           // Add any type-specific logic, and supply the default message.
        }
      
        public MyException(string message): base(message) 
        {
           // Add any type-specific logic.
        }
        public MyException(string message, Exception innerException): 
           base (message, innerException)
        {
           // Add any type-specific logic for inner exceptions.
        }
        protected MyException(SerializationInfo info, 
           StreamingContext context) : base(info, context)
        {
           // Implement type-specific serialization constructor logic.
        }
      }  
      

    or

        [Serializable()]
        public sealed class MyException : Exception
        {
          public MyException()
          {
             // Add any type-specific logic, and supply the default message.
          }
    
          public MyException(string message): base(message) 
          {
             // Add any type-specific logic.
          }
          public MyException(string message, Exception innerException): 
             base (message, innerException)
          {
             // Add any type-specific logic for inner exceptions.
          }
          private MyException(SerializationInfo info, 
             StreamingContext context) : base(info, context)
          {
             // Implement type-specific serialization constructor logic.
          }
        }  
    
    0 讨论(0)
  • 2020-11-22 03:12

    You can also do a conditional check with parameters in the constructor, which allows some flexibility.

    public MyClass(object myObject=null): base(myObject ?? new myOtherObject())
    {
    }
    

    or

    public MyClass(object myObject=null): base(myObject==null ? new myOtherObject(): myObject)
    {
    }
    
    0 讨论(0)
  • 2020-11-22 03:15

    If you need to call the base constructor but not right away because your new (derived) class needs to do some data manipulation, the best solution is to resort to factory method. What you need to do is to mark private your derived constructor, then make a static method in your class that will do all the necessary stuff and later call the constructor and return the object.

    public class MyClass : BaseClass
    {
        private MyClass(string someString) : base(someString)
        {
            //your code goes in here
        }
    
        public static MyClass FactoryMethod(string someString)
        {
            //whatever you want to do with your string before passing it in
            return new MyClass(someString);
        }
    }
    
    0 讨论(0)
提交回复
热议问题