TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Base

C# Base for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C

C# Base

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.

C# base keyword: accessing base class field

We can use the base keyword to access the fields of the base class within derived class. It is useful if base and derived classes have the same fields. If derived class doesn't define same field, there is no need to use base keyword. Base class field can be directly accessed by the derived class.

Let's see the simple example of base keyword in C# which accesses the field of base class.

using System;
public class Animal{
    public string color = "white";
}
public class Dog: Animal
{
    string color = "black";
    public void showColor()
    {
        Console.WriteLine(base.color);
        Console.WriteLine(color);
    }
    
}
public class TestBase
{
    public static void Main()
    {
        Dog d = new Dog();
        d.showColor();
    }
}

Output:

white
black

C# base keyword example: calling base class method

By the help of base keyword, we can call the base class method also. It is useful if base and derived classes defines same method. In other words, if method is overridden. If derived class doesn't define same method, there is no need to use base keyword. Base class method can be directly called by the derived class method.

Let's see the simple example of base keyword which calls the method of base class.

using System;
public class Animal{
    public virtual void eat(){
        Console.WriteLine("eating...");
    }
}
public class Dog: Animal
{
    public override void eat()
    {
        base.eat();
        Console.WriteLine("eating bread...");
    }
    
}
public class TestBase
{
    public static void Main()
    {
        Dog d = new Dog();
        d.eat();
    }
}

Output:

eating...
eating bread...

C# inheritance: calling base class constructor internally

Whenever you inherit the base class, base class constructor is internally invoked. Let's see the example of calling base constructor.

using System;
public class Animal{
    public Animal(){
        Console.WriteLine("animal...");
    }
}
public class Dog: Animal
{
    public Dog()
    {
        Console.WriteLine("dog...");
    }
    
}
public class TestOverriding
{
    public static void Main()
    {
        Dog d = new Dog();
        
    }
}

Output:

animal...
dog...
Next TopicC# Polymorphism




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf