Why not use :base to pass all the parameter? [duplicate]

Can somone please tell me what does the syntax below means?

public ScopeCanvas(Context context, IAttributeSet attrs) : base(context, attrs)
{
}

I mean what is method(argument) : base(argument) {} ??

P.S This is a constructor of a class.


The :base syntax is a way for a derived type to chain to a constructor on the base class which accepts the specified argument. If omitted the compiler will silently attempt to bind to a base class constructor which accepts 0 arguments.

class Parent {
  protected Parent(int id) { } 
}

class Child1 : Parent {
  internal Child1() { 
    // Doesn't compile.  Parent doesn't have a parameterless constructor and 
    // hence the implicit :base() won't work
  }
}

class Child2 : Parent {
  internal Child2() : base(42) { 
    // Works great
  }
}

There is also the :this syntax which allows chaining to constructors in the same type with a specified argument list


Your class is likely defined like this:

MyClass : BaseClass

It derives from some other class. : base(...) on your constructor calls the appropriate constructor in the base class before running the code in your derived class's constructor.

Here's a related question.

EDIT

As noted by Tilak, the MSDN documentation on the base keyword provides a good explanation.


to call named constructor of base class. if base( argument ) is not specified, parameterless constructor is called

What really is the purpose of "base" keyword in c#?

Base keyword


It calls the constructor from the base class passing the arguments context and attrs