What does the syntax "base" mean?

Can someone tell me what the syntax below means?

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

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

PS This is a class constructor.

+7
source share
8 answers

The :base syntax is a way for a derived type to bind to the constructor of a base class that takes the specified argument. If omitted, the compiler will silently try to bind to the constructor of the base class, which takes 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 a syntax :this , which allows you to bind constructors to the same type with the specified list of arguments

+17
source

Your class is probably defined as follows:

 MyClass : BaseClass 

This comes from some other class. : base(...) in your constructor calls the corresponding constructor in the base class before running the code in the constructor of the derived class.

Here is a related question.

EDIT

As noted by Tilak , MSDN documentation in key provides a good explanation.

+3
source

to invoke the named constructor of the base class. if base (argument) is not specified, the constructor without parameters is called

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

base keyword

+2
source

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

+2
source

This is an abstract overloaded class constructor that allows you to initialize the arguments for the derived and base class and indicate whether the overloaded constructor should 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) } } 
+2
source

You class is inherited from the base class, and when you initialize an object of type ScopeCanvas, the base constructor is called using the parameter list (context, attrs)

+1
source

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

 Public class BaseType { public BaseType(object something) { } } public class MyType : BaseType { public MyType(object context) : base(context) { } } 
+1
source

In the examples above, everyone is talking about : a base that nobody accepts . Yes, the base is used to access the parent member, but is not limited only to the constructor, we can directly use base._parentVariable or base._parentMethod ().

base. Example

0
source

All Articles