.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

Inheritance in C#

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

          This code demonstrates the use of single inheritance in C#. It the program there are two classes X and Y. Y is extending X here through inheritance. Each of these two classes have there own constructors, but we are passing the value to the base class constructor(X()) through the 'base' keyword.

class X
{
int a, b;
public X(int a1, int b1)
{
a = a1;
b = b1;
Console.WriteLine("a={0}b={1}", a, b);
}
}

//Y extending the base class X
class Y:X
{
public Y(int n, int m):base(n,m)
{
Console.WriteLine("n={0}m={1}",n,m);
}
}

class MainClass
{
static void Main(string[] args)
{
Y o = new Y(10,20);
}
}

OUTPUT

a=10b=20
n=10m=20

     

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