logo search
CSharp_Prog_Guide

Результат 1 Employee number: 101 Employee name: Claude Vige

Example 2

Description

This example demonstrates how to access a property in a base class that is hidden by another property that has the same name in a derived class.

Code

public class Employee

{

private string name;

public string Name

{

get { return name; }

set { name = value; }

}

}

public class Manager : Employee

{

private string name;

// Notice the use of the new modifier:

public new string Name

{

get { return name; }

set { name = value + ", Manager"; }

}

}

class TestHiding

{

static void Main()

{

Manager m1 = new Manager();

// Derived class property.

m1.Name = "John";

// Base class property.

((Employee)m1).Name = "Mary";

System.Console.WriteLine("Name in the derived class is: {0}", m1.Name);

System.Console.WriteLine("Name in the base class is: {0}", ((Employee)m1).Name);

}

}