Take the 2-minute tour ×

I have a class in C# that has two constructors

public class GObject {

    public GObject(){
        // The default constructor

    public GObject(int xPos, int yPos){
        // Second constructor


Is this valid to write a sub-class Block like this?

public class Block : GObject {

    // Sub class methods go here, no special constructor


And instantiate Block with the 2nd constructor?

Block myBlock = new Block(10, 15);
share|improve this question

migrated from programmers.stackexchange.com Jan 8 '13 at 12:38


Not valid. Have you tried to compile this code? You'll get an error: 'XYZ.Block' does not contain a constructor that takes 2 arguments –  Akim Jan 8 '13 at 12:37
Just got a doubt since it worked with empty constructor, I thought it would call the constructor of the base class –  Sri Harsha Chilakapati Jan 8 '13 at 15:32
add comment

2 Answers

up vote 11 down vote accepted

Since you don't have a two parameter constructor defined on Block, you can't write your final line - it will not compile.

You can have a chained constructor on Block:

public Block(int xPos, int yPos) : base(xPos, yPos)

In which, case:

Block myBlock = new Block(10, 15);

Will work just fine.

share|improve this answer
add comment

By default, if you do not write the constructor explicitly; compiler creates a default constructor with no parameters.

In your case, since Block does not have any constructors defined, only parameterless constructor is created. Thus, you can not create a Block object using two parameters.

share|improve this answer
add comment

Your Answer


