views:

151

answers:

4

What is the purpose of base() in the following code?

class mytextbox : TextBox
{
    public mytextbox() : base()
    {
        this.Text = "stack";
    }
} 
+2  A: 

No idea, it is unnecessary. A constructor always calls the base class constructor automatically without you having to write it explicitly. But that's what it means. You'd have to write it if you don't want to call the parameterless constructor but one that takes an argument.

Hans Passant
+10  A: 

base() in your code is a call to the parameterless constructor of the base-class ofmyTextBox, which happens to beTextBox. Note that this base constructor will execute before the execution of the body of the constructor in the derived class.

Every class's constructor must eventually call one of its base class's constructors, either directly or through chained constructors in the same class. Consequently, there is always an implicit / explicit base(...) or explicit this(...) call on every constructor declaration. If it is omitted, there an implicit call to base(), i.e. the parameterless constructor of the base class (this means that the call tobase()in your example is redundant). Whether such a declaration compiles or not depends on whether there exists such an accessible constructor in the base class.

EDIT: Two trivial points:

  1. The root of the class-hierarchy does not have a base class, so this rule does not apply to System.Object's only constructor.
  2. The first sentence should read: "every non-System.Object class's constructor that completes successfully must eventually call one of its base class's constructors." Here is a 'turtles all the way down' example where the base class's contructor is never called: instantiating an object of such a class will obviously overflow the execution-stack.

// Contains implicit public parameterless constructor
public class Base { } 

// Does not contain a constructor with either an explicit or implicit call to base()
public class Derived : Base
{
    public Derived(int a)
        : this() { }

    public Derived()
        : this(42) { }

    static void Main()
    {
        new Derived(); //StackOverflowException
    }
}
Ani
+1 for most accurate answer so far.
tia
@Ani : So in my example is not required?
mystack
@mystack: No, it's redundant.
Ani
A: 

" Every class's constructor must eventually call one of its base class's constructors" It has ambiguity for me, now resolved. see Base and this

SaeedAlg
I think 'eventually' means the base class constructor will get called eventually. You cannot write a constructor in the way that no base class constructor will get called at all.
tia
Saeed, that is incorrect. An implicit `: base()` call exists if none is specified, so a constructor of your base class will be called regardless.
Allon Guralnek
"You cannot write a constructor in the way that no base class constructor will get called at all" the base constructor only calls when u have a drived object (As Aani Edited, yes, because in c# all objects inherited from 'object' implicity this calls will be happened for hand written classes). If we don't care about 'object' as a base of all objects, there is no other base calls for root classes.
SaeedAlg
A: 

Why At design time messages are displayed ؟

my code :

 class Class1:TextBox 
{
 public Class1()
 {
     this.Resize += new EventHandler(Class1_Resize);
 }
  void Class1_Resize(object sender, EventArgs e)
  {
      MessageBox.Show("Resize");
  }
}

alt text

mystack
@mystack: This is not a forum. Please delete this 'answer' and ask it as a separate question. It appears to be a completely different issue anyway. Thanks.
Ani
@Ani: not different.Questions were edited. ،Merci
mystack