Dumbo
Dumbo

Reputation: 14112

What does the "base" syntax mean?

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.

Upvotes: 9

Views: 6141

Answers (8)

Raj Kumar - rajkrs
Raj Kumar - rajkrs

Reputation: 1363

In above examples all are talking about :base no one is taking about base. Yes base is uses to access member of parent but is not limited with constructor only we can use base._parentVariable or base._parentMethod() directly.

base. example

Upvotes: 0

corn3lius
corn3lius

Reputation: 4985

This is an abstract overloaded class constructor which allows for initilizing of arguments for the derived and the base class and specifying if an overloaded constructor is to be used. LINK

public class A
{
    public A()
    { }
    public A(int size)
    { }
};

class B : public A
{
    public B() 
    {// this calls base class constructor A() 
    }
    public B(int size) : base(size)
    { // this calls the overloaded constructor A(size)
    }
}

Upvotes: 2

zimdanen
zimdanen

Reputation: 5626

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.

Upvotes: 3

Oblivion2000
Oblivion2000

Reputation: 616

This means that this constructor takes two arguments, and passes them to the inherited objects constructor. An example below with only one argument.

Public class BaseType
{
    public BaseType(object something)
    {

    }
}

public class MyType : BaseType
{
     public MyType(object context) : base(context)
     {

     } 
}

Upvotes: 1

JaredPar
JaredPar

Reputation: 754655

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

Upvotes: 19

Aheho
Aheho

Reputation: 12821

You class is inheriting from a baseclass, and when you intialize an object of type ScopeCanvas, the base constructor is called with a parameter list of (context, attrs)

Upvotes: 1

vguzmanp
vguzmanp

Reputation: 825

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

Upvotes: 2

Tilak
Tilak

Reputation: 30698

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

Upvotes: 2

Related Questions