Jonas Stawski

Everything .NET and More

Behavior of Default Constructors and Base Classes

A client of mine requires us to use ReSharper. I usually don't like to use any of those tools because if I get used to then and I go crazy when I do not have them, but I think I will give ReSharper a chance at my own dev machine. One of the cool features about ReSharper is that it marks the state of the current class with a green, yellow, or red indicator specifying whether the class is not compileable, compileable with warnings, or compileable without warnings. While working on a class ReSharper was marking it with yellow and specifying that I had a "Redundant base constructor call." The code looked something like this:

 public class base1
 {
  public base1(int number)
  {
  }
 }

public class base2 : base1
 {
  public base2() : base(1)
  {
  
  }
 }

public class Class1 : base2
 {
  public Class1() : base()
  {
   
  }

  public Class1(string text)
  {
  }
 }

ReSharper will mark the base() call of the default constructor of Class1 as a redundant call and I wasn't sure of it. I was affraid that if i didn't specify to call the base constructor that it will ignore it alltogher and therefore the constructor of the lowest base class will never get called. It turns out that ReSharper is right and you don't need to specify the call to the base() constructor when calling it from the default constructor because it does that by default. Go ahead and try it. Copy the code above and remove the ": base()" from Class1, step through it, and you will see the default constructor of the base class is called anyway.

Every day you learn something new. Happy Programming!

 

 

Comments (1) -

Hey Now Jonas,
That is good to know.
Thx 4 the info,
Catto

Reply

Add comment

biuquote
Loading