← Back to C# Course | Chapter 7: Inheritance | Lesson 2 of 6

base Keyword क्या है

base keyword किसी child class को अपने parent से बात करने देता है, जैसे mom से पूछना कि उसने कुछ कैसे किया। यह किसी method या constructor का parent का version तक पहुंचता है।

In this page:

  1. The base Keyword
Syntax
csharp
class DerivedClass : BaseClass
{
    public DerivedClass(type parameter) : base(parameter)   // call the base constructor
    {
    }

    public override void MethodName()
    {
        base.MethodName();   // call the base version
    }
}

The base Keyword

base किसी derived class के अंदर से immediate parent class को refer करता है, आपको base constructor या किसी overridden base method को explicitly call करने देते हुए।

किसी constructor में base(args) initialization काम को parent को forward करता है। यह base class द्वारा पहले से handle किए logic को दोहराने से बचाता है।

Note: किसी override के अंदर जब आप parent के behavior को replace नहीं बल्कि extend करना चाहें तब base.MethodName() उपयोग करें।

उदाहरण: The base Keyword

markup
using System;

class Animal
{
    public string Name;

    public Animal(string name)
    {
        Name = name;
    }

    public virtual void Describe()
    {
        Console.WriteLine($"I am {Name}");
    }
}

class Dog : Animal
{
    public Dog(string name) : base(name) { }

    public override void Describe()
    {
        base.Describe();
        Console.WriteLine("...and I am a dog");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Dog d = new Dog("Rex");
        d.Describe();
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. base class के पास कोई default constructor न होने पर सही arguments के साथ base constructor call करना भूल जाना।
  2. किसी static method में base उपयोग करना, जहां यह उपलब्ध नहीं है।
  3. किसी abstract method के लिए base.Method() call करना, जो एक compile error है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.