Skip to main content

How to Call Base Class Method from Derived Class


C# - Basics - Classes - Inheritance - How Calling Base Class Method from Derived Class

Calling Base Class method from Derived Class in C#


This topic contains and tries to cover following subjects:
- Explanation of Base keyword in C#
- Syntax of Base keyword in C#
- Example of Base keyword in C#

Articles provides solution to following questions and issues:
- How to use base keyword in C#
- How to call a method of base Class from the derived Class
- Why would a programmer need to call a base Class method from derived Class in practice

Previously Explained about  How to convert json to class in c#

Explanation of Base keyword in C#

Base keyword in C# enables programmers to call a method of Base Class from the derived Class.

Syntax of Base keyword in C#

Following syntax is used to call a method of base Class from the derived Class:
namespace  NS_hardwareManagerApp

{

    //declare a base Class...

    class CL_aBaseClass

    {

        public void FN_Info()

        { 

          
        }

    }

    //create a derived Class and inherit base Class...

    class CL_aDerivedClass : CL_aBaseClass

    {

        public void FN_Info()

        {

            //call base Class method in derived Class...

            base.FN_Info();

        }

    }

    class CL_x

    {

        static void Main(string [] args)

        {
           
           Console.ReadLine();

        }

    }

}

Example of Base keyword in C#


Following example indicates how to call a base Class method from derived Class. Example is a console application which is created and test with Visual Studio 2008.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace  NS_HRapplication

{

    //declare a base Class...

    class CL_employees

    {

        public void FN_displaySalary()

        {

            Console.WriteLine("Salary is 1000 euro...");

        }

    }

    //create a derived Class and inherit base Class...

    class CL_tehnicians : CL_employees

    {

        public void FN_displaySalary()

        {

            //we assume that managers are getting paid 1000 euro extra

              than base employee salary...

            base.FN_displaySalary();

        }
    }

    class CL_x

    {
        static void Main(string [] args)

        {

            //create a technician instance-object...  
          CL_tehnicians o_aTechnician = new CL_tehnicians();

            //display salary...

            o_aTechnician.FN_displaySalary();

            Console.ReadLine();

        }

    }

}

Application output:
C# - Basics - Classes - Inheritance - Calling Base Class Method from Derived Class

Comments

Popular posts from this blog

Asp.Net AjaxFileUpload Control With Drag Drop And Progress Bar

This Example explains how to use AjaxFileUpload Control With Drag Drop And Progress Bar Functionality In Asp.Net 2.0 3.5 4.0 C# And VB.NET. Previous Post  I was Explained about the   jQuery - Allow Alphanumeric (Alphabets & Numbers) Characters in Textbox using JavaScript  ,  Fileupload show selected file in label when file selected  ,  Check for file size with JavaScript before uploading  . May 2012 release of AjaxControlToolkit includes a new AjaxFileUpload Control  which supports Multiple File Upload, Progress Bar and Drag And Drop functionality. These new features are supported by Google Chrome version 16+, Firefox 8+ , Safari 5+ and Internet explorer 10 + , IE9 or earlier does not support this feature. To start with it, download and put latest AjaxControlToolkit.dll in Bin folder of application, Place ToolkitScriptManager  and AjaxFileUpload on the page. HTML SOURCE < asp:ToolkitScriptManager I...

Check dot net core framework version in my PC Or System

 Open My Computer → double click "C:" drive → double click "Windows" → double click "Microsoft.NET" → double click "Framework" → Inside this folder, there will be folder(s) like "v1.0.3705" and/or "v2.0.50727" and/or "v3.5" and/or "v4.0.30319". Your latest .NET version would be in the highest v number folder, so if v4.0.30319 is available that would hold your latest .NET framework. However, the v4.0.30319 does not mean that you have the .NET framework version 4.0. The v4.0.30319 is your Visual C# compiler version, therefore, in order to find the .NET framework version do the following. Go to a command prompt and follow this path: C:\Windows\Microsoft.NET\Framework\v4.0.30319 (or whatever the highest v number folder) C:\Windows\Microsoft.NET\Framework\v4.0.30319 > csc.exe Output: Microsoft (R) Visual C# Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 Copyright (C) Microsoft Corporati...

ASP.NET Routing

ASP.NET routing enables you to use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users. The ASP.NET MVC framework and ASP.NET Dynamic Data extend routing to provide features that are used only in MVC applications and in Dynamic Data applications. For more information about MVC, see ASP.NET MVC 3 . For more information about Dynamic Data, see ASP.NET Dynamic Data Content Map . In an ASP.NET application that does not use routing, an incoming request for a URL typically maps to a physical file that handles the request, such as an .aspx file. For example, a request for http://server/application/Products.aspx?id=4 maps to a file that is named Products.aspx that contains code and markup for rendering a response to the browser. The Web page uses the query string value of id=4 to determine what type of c...