Windows Phone Developers

Friday, December 5, 2008

Base Class Constructors in C#

Base-class constructor can be called when creating instances of the derived class using the base keyword as shown below

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace CSharpSample

{

class CBaseProduct : Object

{

public string ProductSKU ;

public string ProductName ;

public CBaseProduct(string SKUID, string ProdName)

{

this.ProductSKU = SKUID;

this.ProductName = ProdName;

}

}

class CSpecProduct : CBaseProduct

{

public string ProductRate;

public CSpecProduct(string SKUID, string ProdName, string ProdRate)

: base(SKUID, ProdName)

{

this.ProductRate = ProdRate;

}

}

}

A base class access is permitted only in a constructor, an instance method, or an instance property accessor.

The following code calls the derived class constructor, which in turn calls the base class constructor

CSpecProduct c1 = new CSpecProduct("00123", "Tata Tea", "45.00");

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment