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...
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: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 -
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,With these definitions,
would output
You can use base to fill values in the constructor of an object's base class.
Example:
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
2) Example
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: