views:

341

answers:

6

Thus for used base class for some commom reusable methods in every page of my application...

public class BaseClass:System.Web.UI.Page
{
   public string GetRandomPasswordUsingGUID(int length)
   {
      string guidResult = System.Guid.NewGuid().ToString();
      guidResult = guidResult.Replace("-", string.Empty);
      return guidResult.Substring(0, length);
   }
}

So if i want to use this method i would just do,

public partial class forms_age_group : BaseClass
{
      protected void Page_Load(object sender, EventArgs e)
      {
            //i would just call it like this
            string pass = GetRandomPasswordUsingGUID(10);
      }
}

It does what i want but there is a "Base" keyword that deals with base class in c# ... I really want to know when should use base keyword in my derived class....

Any good example...

+11  A: 

The base keyword is used to refer to the base class when chaining constructors or when you want to access a member (method, property, anything) in the base class that has been overridden or hidden in the current class. For example,

class A {
    protected virtual void Foo() {
        Console.WriteLine("I'm A");
    }
}

class B : A {
    protected override void Foo() {
        Console.WriteLine("I'm B");
    }

    public void Bar() {
        Foo();
        base.Foo();
    }
}

With these definitions,

new B().Bar();

would output

I'm B
I'm A
Matti Virkkunen
It's not limited to functions. Wherever there is ambiguity in classes (parent and children), base keyword clarifies which method/data member the user is talking about.
Nayan
@Nayan: Good point. Revised to say member instead.
Matti Virkkunen
A: 

Base is used when you override a method in a derived class but just want to add additional functionality on top of the original functionality

For example:

  // Calling the Area base method:
  public override void Foo() 
  {
     base.Foo(); //Executes the code in the base class

     RunAdditionalProcess(); //Executes additional code
  }
Jasper
+4  A: 

If you have the same member in class and it's super class, the only one way to call member from super class - using base keyword:

protected override void OnRender(EventArgs e)
{
   // do something

   base.OnRender(e);

   // just OnRender(e); will bring a StakOverFlowException
   // because it's equal to this.OnRender(e);
}
abatishchev
A: 

The base keyword is used to access members in the base class that have been overridden (or hidden) by members in the subclass.

For example:

public class Foo
{
    public virtual void Baz()
    {
        Console.WriteLine("Foo.Baz");
    }
}

public class Bar : Foo
{
    public override void Baz()
    {
        Console.WriteLine("Bar.Baz");
    }

    public override void Test()
    {
        base.Baz();
        Baz();
    }
}

Calling Bar.Test would then output:

Foo.Baz;
Bar.Baz;
Will Vousden
+1  A: 

You will use base keyword when you override a functionality but still want the overridden functionality to occur also.

example:

 public class Car
 {
     public virtual bool DetectHit() 
     { 
         detect if car bumped
         if bumped then activate airbag 
     }
 }


 public class SmartCar : Car
 {
     public override bool DetectHit()
     {
         bool isHit = base.DetectHit();

         if (isHit) { send sms and gps location to family and rescuer }

         // so the deriver of this smart car 
         // can still get the hit detection information
         return isHit; 
     }
 }


 public sealed class SafeCar : SmartCar
 {
     public override bool DetectHit()
     {
         bool isHit = base.DetectHit();

         if (isHit) { stop the engine }

         return isHit;
     }
 }
Michael Buen
+1 for if(isHit) {remove the i and it takes on a whole different meaning} ;)
andrewWinn
@andrewWinn: that could be the primary reason why C# language designers cannot introduce VB.NET's IsNot keyword to C# language. It will have an appearance of booger :-D heheh
Michael Buen
A: 

You can use base to fill values in the constructor of an object's base class.

Example:

public class Class1
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime Birthday { get; set; }

    public Class1(int id, string name, DateTime birthday)
    {
        ID = id;
        Name = name;
        Birthday = birthday;
    }
}

public class Class2 : Class1
{
    public string Building { get; set; }
    public int SpotNumber { get; set; }
    public Class2(string building, int spotNumber, int id, 
        string name, DateTime birthday) : base(id, name, birthday)
    {
        Building = building;
        SpotNumber = spotNumber;
    }
}

public class Class3
{
    public Class3()
    {
        Class2 c = new Class2("Main", 2, 1090, "Mike Jones", DateTime.Today);
    }
}
Jon