Base keyword In C# PDF Print E-mail
User Rating: / 0
PoorBest 
Thursday, 05 March 2009
HTML clipboard

The keyword base is used to access members of the base class from inside a derived class.

Key Points:
Calling a method from the base class is possible even if it has been overriden by another method by using the keyword base. Also, when you are creating a instance of a derived class you can call the base classes contructor if so desired with this keyword, base.

There rules governing base class access are (or permissible in):

  • in a constructor
  • in an instance method
  • instance property accessor

Attemps at using the keyword base in a static method will result in a error.

Example:
This brief example displays the concept of calling the base classes method even though it has been overridden in the derived class!

  <%@ Page language="c#" %>
			<script language="C#" runat="server">
			public class CSharp
			{        
			public virtual string GetMessage()
			{
			return "&ltbr>All your base are belong to us!";    
			}
			}
			// Class derived from CSharp class (above)
			public class Friends: CSharp
			{
			// overrides CSharp classes method
			public override string GetMessage()
			{
			return base.GetMessage();
			}
			}
			public void Page_Load(object sender, EventArgs e)
			{
			Friends CSF = new Friends();
			// Friend's method GetMessage calls base classes method
			Response.Write(CSF.GetMessage());
			}
			</script>
			

You can call a base classes contructor from within a derived classes contructor as follows (below is the derived classes constructor):

			public DerivedClassesConstructor() : base()
			{
			}
			
 
 
< Prev   Next >
School Joomla Templates and Joomla Tutorials