Thursday, July 23, 2009

Use of basic keywords in c#

In C# base keyword is used to call the base class constructor and base class field.

First the values are received by the derived class constructor and then passed to the base class constructor.

See The Code

class A
{
int i;
A(int n, int m)
{
x = n;
y = m
Console.WriteLine("n="+x+"m="+y);
}
}
class B:A
{
int i;
B(int a, int b):base(a,b)//calling base class constructor and passing value
{
base.i = a;//passing value to base class field
i = b;
}
public void Show()
{
Console.WriteLine("Derived class i="+i);
Console.WriteLine("Base class i="+base.i);
}
}
class MainClass
{
static void Main(string args[])
{
B b=new B(5,6);//passing value to derive class constructor
b.Show();
}
}


OUTPUT

n=5m=6
Derived class i=6
Base class i=5

No comments: