.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Use of base keyword in C#

Posted By:Abhisek Panda       Posted Date: October 27, 2009    Points: 10    Category: Visual Studio    URL: http://www.dotnetspark.com  
 

Base Keyword

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

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend