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...
And C++ doesn't have a super or base keyword to designate “the base class”, like C# and Java do. One reason for this is that C++ supports multiple inheritance, which would make such a keyword ambiguous. But on the other hand, multiple inheritance is not used so often in C++.
In C#, base keyword is used to access fields, constructors and methods of base class. You can use base keyword within instance method, constructor or instance property accessor only. You can't use it inside the static method.
In C# terms, the base constructor is executed first.
What is a Base Class? In an object-oriented programming language, a base class is an existing class from which the other classes are determined and properties are inherited. It is also known as a superclass or parent class.
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
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With