8

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • 2
    That's a constuctor, not a method. – Gabe May 08 '12 at 17:15
  • 2
    I believe that if a question has more than 5 answers in less than two minutes, it shouldn't have been asked. @Sean87 you could easily google "C# base keyword" – SimpleVar May 08 '12 at 17:17
  • 3
    [Google would give you much faster answer](https://www.google.com/search?q=C%23+base&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a) then StackOverflow. (@Yorye Nathan - exactly my point.) – zmilojko May 08 '12 at 17:18
  • 3
    I know this is old, but I did google 'C# :base keyword' and got here... Of course the MSDN link was first, but SO answers are usually more succinct. – ledgeJumper Jul 29 '13 at 14:47

8 Answers8

19

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

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • **"If omitted the compiler will silently attempt to bind to a base class constructor which accepts 0 arguments."** This is the most critical part of the explanation - thanks. – Sabuncu Aug 31 '15 at 16:48
3

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.

Community
  • 1
  • 1
zimdanen
  • 5,508
  • 7
  • 44
  • 89
2

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

Community
  • 1
  • 1
Tilak
  • 30,108
  • 19
  • 83
  • 131
2

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

vguzmanp
  • 785
  • 1
  • 10
  • 31
2

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)
    }
}
corn3lius
  • 4,857
  • 2
  • 31
  • 36
1

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)

Aheho
  • 12,622
  • 13
  • 54
  • 83
1

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)
     {

     } 
}
Oblivion2000
  • 616
  • 4
  • 9
0

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

Raj kumar
  • 1,275
  • 15
  • 20