How to use base keyword with example ? [Resolved]

Posted by Akiii under C# on 6/8/2012 | Points: 10 | Views : 10303 | Status : [Member] | Replies : 9
Hi,

Can anyone tell me how to call a Base Class Functionality by base keyword from a derived class ?



Thanks and Regards
Akiii




Responses

Posted by: Ajay.Kalol on: 6/8/2012 [Member] Starter | Points: 50
Posted by: perfectchourasia-9163 on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down
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.



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

if it is correct then check it resolved.

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi @CGN007..

you have a good example but can you tell me one thing ? Is base keyword only used when we have virtual and override keyword ?


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
@sandeep ....there is some problem in your code. Please check it and try to post your code in code blocks !


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
@Ajay.....let me check your example and i will let you know !


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
@Ajay.Kalol.....The link that you provided is very good !


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
@CGN007......i think you are talking about constructor chaining !


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
@CGN007 ....yes I got your point. Thanks for the help !


Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ajay.Kalol on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down
Always WelCome

Ajay
ajaypatelfromsanthal.blogspot.in

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response