Search This Blog

Monday, March 25, 2013

base keyword C#

C# > Keywords > Base

Base is used to access members of the base class from within a derived class

Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public class Animal
        {
            protected string type = "";
            public virtual string GetAnimalInfo()
            {
               return(type); 
            }
        }
        class Dog : Animal
        {
            private string name = "";
            public Dog(string name)
            {
                base.type = "dog";
                this.name = name;
            }
            public string GetAnimalInfo()
            {
                // Calling the base class GetInfo method:
                string type = base.GetAnimalInfo();
                return (name + " is a " + type);
            }
        }
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            Dog dog = new Dog("Tashi");
            string AnimalInfo = dog.GetAnimalInfo(); // "Tashi is a dog"
         }
    }
}