FunctionX Logo

C# Examples: The new Modifier

 

Introduction

Besides allocating memory for a new reference object, C# uses the new keyword to hide the implementation of a method or property from the member that has the same name in a base class. Here is an example of using it:

using System;

public class Square
{
	private double _side;
	
	public double Side
	{
		get { return (_side < 0) ? 0.00 : _side; }
		set { _side = value; }
	}

	public double Area
	{
		get { return _side * _side; }
	}
}

public class Cube : Square
{
	new public double Area
	{
		get { return this.Side * this.Side * this.Side; }
	}
}

public class Exercise
{
	static void ShowCube(Cube faces)
	{
		Console.WriteLine("Characteristics of the cube");
		Console.WriteLine("Side: {0}", faces.Side);
		Console.WriteLine("Area: {0}", faces.Area);
	}

	static void Main()
	{
		Cube SixFaces = new Cube();

		SixFaces.Side = 24.55;
		ShowCube(SixFaces);
	}
}

This would produce:

Characteristics of the cube
Side: 24.55
Area: 14796.346375
 

Home Copyright 2004 FunctionX, Inc.