<p>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?</p>

<p>For example, if I inherit from the Exception class I want to do something like this:</p>

<pre><code>class MyExceptionClass : Exception
{
     public MyExceptionClass(string message, string extraInfo)
     {
         //This is where it's all falling apart
         base(message);
     }
}
</code></pre>

<p>Basically what I want is to be able to pass the string message to the base Exception class.</p>

[{'answer_id': 12052, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 12, 'selected': True, 'text': "<p>Modify your constructor to the following so that it calls the base class constructor properly:</p>\n\n<pre><code>public class MyExceptionClass : Exception\n{\n    public MyExceptionClass(string message, string extrainfo) : base(message)\n    {\n        //other stuff here\n    }\n}\n</code></pre>\n\n<p>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.</p>\n"}, {'answer_id': 1844965, 'author': 'SnowBEE', 'author_id': 224505, 'author_profile': 'https://Stackoverflow.com/users/224505', 'pm_score': 5, 'selected': False, 'text': '<pre><code>public class MyExceptionClass : Exception\n{\n    public MyExceptionClass(string message,\n      Exception innerException): base(message, innerException)\n    {\n        //other stuff here\n    }\n}\n</code></pre>\n\n<p>You can pass inner exception to one of the constructors.</p>\n'}, {'answer_id': 2731807, 'author': 'Axl', 'author_id': 16605, 'author_profile': 'https://Stackoverflow.com/users/16605', 'pm_score': 9, 'selected': False, 'text': '<p>Note that you can use <strong>static</strong> methods within the call to the base constructor.</p>\n\n<pre><code>class MyExceptionClass : Exception\n{\n     public MyExceptionClass(string message, string extraInfo) : \n         base(ModifyMessage(message, extraInfo))\n     {\n     }\n\n     private static string ModifyMessage(string message, string extraInfo)\n     {\n         Trace.WriteLine("message was " + message);\n         return message.ToLowerInvariant() + Environment.NewLine + extraInfo;\n     }\n}\n</code></pre>\n'}, {'answer_id': 15103087, 'author': 'aalimian', 'author_id': 1490214, 'author_profile': 'https://Stackoverflow.com/users/1490214', 'pm_score': 7, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>public class MyClass : BaseClass\n{\n    private MyClass(string someString) : base(someString)\n    {\n        //your code goes in here\n    }\n\n    public static MyClass FactoryMethod(string someString)\n    {\n        //whatever you want to do with your string before passing it in\n        return new MyClass(someString);\n    }\n}\n</code></pre>\n'}, {'answer_id': 19905310, 'author': 'Janus Pedersen', 'author_id': 1032159, 'author_profile': 'https://Stackoverflow.com/users/1032159', 'pm_score': 5, 'selected': False, 'text': '<p>It is true use the <code>base</code> (something) to call the base class constructor, but in case of overloading use the <code>this</code> keyword</p>\n\n<pre><code>public ClassName() : this(par1,par2)\n{\n// do not call the constructor it is called in the this.\n// the base key- word is used to call a inherited constructor   \n} \n\n// Hint used overload as often as needed do not write the same code 2 or more times\n</code></pre>\n'}, {'answer_id': 34973293, 'author': 'Fab', 'author_id': 5328150, 'author_profile': 'https://Stackoverflow.com/users/5328150', 'pm_score': 5, 'selected': False, 'text': '<p><strong>From <a href="https://books.google.co.th/books?id=39d1wZ598ecC&amp;pg=PT394&amp;lpg=PT394&amp;dq=exception%20fxcop&amp;source=bl&amp;ots=bO6nGDVi4f&amp;sig=DFZ7ucCPK__LvIzTVaTSZ2rlXpY&amp;hl=fr&amp;sa=X&amp;ved=0ahUKEwi88-qO9MHKAhUPH44KHVmdCh8Q6AEIUDAG#v=onepage&amp;q=exception%20fxcop&amp;f=false">Framework Design Guidelines</a> and FxCop rules.</strong>:</p>\n\n<p><strong>1. Custom Exception should have a name that ends with Exception</strong></p>\n\n<pre><code>    class MyException : Exception\n</code></pre>\n\n<p><strong>2. Exception should be public</strong></p>\n\n<pre><code>    public class MyException : Exception\n</code></pre>\n\n<p><strong>3. <a href="https://msdn.microsoft.com/en-us/library/ms182151.aspx">CA1032: Exception should implements standard constructors.</a></strong></p>\n\n<ul>\n<li>A public parameterless constructor.</li>\n<li>A public constructor with one string argument.</li>\n<li>A public constructor with one string and Exception (as it can wrap another Exception).</li>\n<li><p>A serialization constructor protected if the type is not sealed and private if the type is sealed. \nBased on <a href="https://msdn.microsoft.com/en-us/library/ms182151.aspx">MSDN</a>:</p>\n\n<pre><code>[Serializable()]\npublic class MyException : Exception\n{\n  public MyException()\n  {\n     // Add any type-specific logic, and supply the default message.\n  }\n\n  public MyException(string message): base(message) \n  {\n     // Add any type-specific logic.\n  }\n  public MyException(string message, Exception innerException): \n     base (message, innerException)\n  {\n     // Add any type-specific logic for inner exceptions.\n  }\n  protected MyException(SerializationInfo info, \n     StreamingContext context) : base(info, context)\n  {\n     // Implement type-specific serialization constructor logic.\n  }\n}  \n</code></pre></li>\n</ul>\n\n<p>or</p>\n\n<pre><code>    [Serializable()]\n    public sealed class MyException : Exception\n    {\n      public MyException()\n      {\n         // Add any type-specific logic, and supply the default message.\n      }\n\n      public MyException(string message): base(message) \n      {\n         // Add any type-specific logic.\n      }\n      public MyException(string message, Exception innerException): \n         base (message, innerException)\n      {\n         // Add any type-specific logic for inner exceptions.\n      }\n      private MyException(SerializationInfo info, \n         StreamingContext context) : base(info, context)\n      {\n         // Implement type-specific serialization constructor logic.\n      }\n    }  \n</code></pre>\n'}, {'answer_id': 36475682, 'author': 'Donat Sasin', 'author_id': 5097657, 'author_profile': 'https://Stackoverflow.com/users/5097657', 'pm_score': 4, 'selected': False, 'text': '<pre><code>public class MyException : Exception\n{\n    public MyException() { }\n    public MyException(string msg) : base(msg) { }\n    public MyException(string msg, Exception inner) : base(msg, inner) { }\n}\n</code></pre>\n'}, {'answer_id': 37492479, 'author': 'dynamiclynk', 'author_id': 1427166, 'author_profile': 'https://Stackoverflow.com/users/1427166', 'pm_score': 4, 'selected': False, 'text': '<p>You can also do a conditional check with parameters in the constructor, which allows some flexibility.</p>\n\n<pre><code>public MyClass(object myObject=null): base(myObject ?? new myOtherObject())\n{\n}\n</code></pre>\n\n<p>or</p>\n\n<pre><code>public MyClass(object myObject=null): base(myObject==null ? new myOtherObject(): myObject)\n{\n}\n</code></pre>\n'}, {'answer_id': 42786257, 'author': 'CShark', 'author_id': 3033876, 'author_profile': 'https://Stackoverflow.com/users/3033876', 'pm_score': 4, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>public class MyException : Exception\n{\n    public MyException(string message, string extraInfo) : base(message)\n    {\n    }\n}\n</code></pre>\n\n<p>I note that in your example you never made use of the <code>extraInfo</code> parameter, so I assumed you might want to concatenate the <code>extraInfo</code> string parameter to the <code>Message</code> property of your exception (it seems that this is being ignored in the accepted answer and the code in your question).</p>\n\n<p>This is simply achieved by invoking the base class constructor, and then updating the Message property with the extra info.</p>\n\n<pre><code>public class MyException: Exception\n{\n    public MyException(string message, string extraInfo) : base($"{message} Extra info: {extraInfo}")\n    {\n    }\n}\n</code></pre>\n'}, {'answer_id': 60657371, 'author': 'springy76', 'author_id': 442376, 'author_profile': 'https://Stackoverflow.com/users/442376', 'pm_score': 3, 'selected': False, 'text': '<p>Using newer C# features, namely <code>out var</code>, you can get rid of the static factory-method.\nI just found out (by accident) that out var parameter of methods called inse base-"call" flow to the constructor body.</p>\n\n<p>Example, using this base class you want to derive from:</p>\n\n<pre><code>public abstract class BaseClass\n{\n    protected BaseClass(int a, int b, int c)\n    {\n    }\n}\n</code></pre>\n\n<p>The non-compiling pseudo code you want to execute:</p>\n\n<pre><code>public class DerivedClass : BaseClass\n{\n    private readonly object fatData;\n\n    public DerivedClass(int m)\n    {\n        var fd = new { A = 1 * m, B = 2 * m, C = 3 * m };\n        base(fd.A, fd.B, fd.C); // base-constructor call\n        this.fatData = fd;\n    }\n}\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>public class DerivedClass : BaseClass\n{\n    private readonly object fatData;\n\n    public DerivedClass(int m)\n        : base(PrepareBaseParameters(m, out var b, out var c, out var fatData), b, c)\n    {\n        this.fatData = fatData;\n        Console.WriteLine(new { b, c, fatData }.ToString());\n    }\n\n    private static int PrepareBaseParameters(int m, out int b, out int c, out object fatData)\n    {\n        var fd = new { A = 1 * m, B = 2 * m, C = 3 * m };\n        (b, c, fatData) = (fd.B, fd.C, fd); // Tuples not required but nice to use\n        return fd.A;\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/12051', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/493/']