easiest way to make a class inherit constructors (C#)

15,433

Solution 1

What you're looking for is the base keyword. You can use it to call into the base constructor and provide the necessary arguments.

Try the following. I picked 0 as a default for lack of a better option

class spleen : foo { 
  public spleen() : base(0,0,0,0)
  {
  }
}

EDIT

Based on your new version of the code, the simplest way to call the constructor is to quite literally do nothing. The default constructor generated for spleen will automatically call the base empty constructor of foo.

class spleen : foo { 
  public spleen() {}
}

Solution 2

You just create it, and call the base constructor:

public spleen(int bar, int wee, int someotherint, int someyetanotherint)
    : base(bar,wee,someotherint,someyetanotherint)
{
    // Do any spleen specific stuff here
}

Solution 3

JaredPar is right but missed something and I don't have enough rep to edit it.

class spleen : foo 
{
     public spleen() : base()
}

If you parent class took in parameter in the constructor then it would be

class foo
{

     public int bar, wee, someotherint, someyetanotherint;

     public foo(int bar, int wee, int someotherint, int someyetanotherint)
     {
           this.bar = bar;
           this.wee = wee;
           this.someotherint = someotherint;
           this.someyetanotherint = someyetanotherint;

     }
}

class spleen : foo 
{
     public spleen(int bar, 
                   int wee, 
                   int someotherint, 
                   int someyetanotherint) : base(bar, 
                                                 wee, 
                                                 someotherint,  
                                                 someyetanotherint)
}
Share:
15,433
Admin
Author by

Admin

Updated on August 03, 2022

Comments

  • Admin
    Admin over 1 year

    Yeah, sorry about asking a stupid n00b question. So I have a C# program. I have a class

    class foo
    {
    
        public int bar, wee, someotherint, someyetanotherint;
    
        public foo()
        {
             this.bar = 1:
             this.wee = 12;
             this.someotherint = 1231;
             this.someyetanotherint = 443;
        }
    }
    

    And I want to make a class called spleen that inherits from foo

    class spleen : foo
    {
    
    }
    

    What is the quickest, easiest syntax to make the class spleen inherit the constructor from foo without having to copy/paste the entire constructor from foo? I don't want to hear that I already have too many parameters. I already know that. (edit: Actually, no. I'm an idiot) I know I should probably somehow call the parent constructor, but I don't know how. How do I do that.

    Edit: I realize now that I should have taken more time to write my question. It looks like I was trying to ask two questions at the same time without realizing it (how to inherit constructors with no parameters, and how to inherit constructors with parameters) , and somehow jumbled it all up. However the answers provided were very helpful and solved my problem. Thanks, and sorry for being such an idiot!

  • jrista
    jrista almost 15 years
    The constructors of the base class do not need to be protected. Any public or protected constructor may be called by a child class in C#.
  • JaredPar
    JaredPar almost 15 years
    Hmm, looks like the user editted the question. My answer matches the original version of the question. You're correct on the updated one though.
  • Kyle Sonaty
    Kyle Sonaty almost 15 years
    Yeah I just looked at the revisions and you had it the first time.
  • Jeremy McGee
    Jeremy McGee almost 15 years
    And this spleen class doesn't even need a constructor - just declaring the class to inherit from foo will automatically call the default constructor of the base class.