What really is the purpose of “base” keyword in c#

2019-01-16 13:47发布

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...

标签: c# keyword base
8条回答
三岁会撩人
2楼-- · 2019-01-16 14:28

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);
}
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-16 14:28

The real purpose of the “base” keyword in c# is as follows: suppose you want to call only the parent class' parameterized constructor - then you can use base and pass the parameters, please see below example...

Example -

 class Clsparent
{
    public Clsparent()
    {
        Console.WriteLine("This is Clsparent class constructor");
    }
    public Clsparent(int a, int b)
    {
        Console.WriteLine("a value is=" + a + " , b value is=" + b);
    }
}
class Clschild : Clsparent
{
    public Clschild() : base(3, 4)
    {
        Console.WriteLine("This is Clschild class constructor");
    }
}
class Program
{
    static void Main(string[] args)
    {
        Clschild objclschild = new Clschild();
        Console.Read();
    }
}
查看更多
The star\"
4楼-- · 2019-01-16 14:30

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
查看更多
放荡不羁爱自由
5楼-- · 2019-01-16 14:30

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);
    }
}
查看更多
干净又极端
6楼-- · 2019-01-16 14:41

Generally, we are using the base class to reuse the property or methods in child class of the base class, so we no need to repeat the same property and methods again in the child class.

Now, we use the base keyword to call a constructor or method from base class directly.

Example

public override void ParentMethod() 
  {
     base.ParentMethod(); //call the parent method

     //Next code.
  }

2) Example

class child: parent
{
    public child() : base(3, 4) //if you have parameterised constructor in base class
    {

    }
}
查看更多
看我几分像从前
7楼-- · 2019-01-16 14:44

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
  }
查看更多
登录 后发表回答