Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An explanation about base..ctor()

Tags:

c#

I'm getting an application from my friends to develop. Then in the code I got confusing code like this:

public someNameHere()
{
   base..ctor();
} 

Well I've never had an application like this. I need an explanation for this

base..ctor();

I've googling and can't find answer that satisfy me. I appreciate for any help you give me.

like image 860
Harits Fadillah Avatar asked Apr 23 '13 08:04

Harits Fadillah


People also ask

What does base () do C#?

base (C# Reference) The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.

What is ctor programming?

In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

What is base constructor in C#?

In C#, both the base class and the derived class can have their own constructor. The constructor of a base class used to instantiate the objects of the base class and the constructor of the derived class used to instantiate the object of the derived class.

Why is base constructor called first in C#?

So if in the initialization process of child class there may be the use of parent class members. This is why the constructor of the base class is called first to initialize all the inherited members.


2 Answers

.ctor() is the internal name used by constructors. It is not used that way in C#, and does not compile. More typically, base..ctor() is only used by the compiler when doing things like:

public class Foo : Bar {
    public Foo(string s) : base(s) {...} 
    public Foo() {...} // has an implicit :base()
}

The only times I've seen that done differently is when decompiling (via reflector etc) some non-C# IL that does the construction code in a different sequence to how the C# compiler does it, which is therefore not expressable in "pure" C#. In raw IL (and from C++ etc) you can call the base-constructor at any point in the constructor - not just at the start.

like image 166
Marc Gravell Avatar answered Oct 22 '22 16:10

Marc Gravell


Should be the default parameterless constructor of your base class. Elaborating, .ctor() is the alias for constructor automatically generated compiling C#. So Writing base..ctor() is referencing the .ctor() method in the base class.

like image 27
misleadingTitle Avatar answered Oct 22 '22 17:10

misleadingTitle