public static void MainX() { var emp = new Employee(); emp.SetSalary(-1000); //этот метод вызовет runtime exception. //emp.Salary = -1000; //обращаться к Salary можно только изнутри класса Employee. //Console.WriteLine(emp.Salary); //так делать тоже нельзя. Как же получить это значение? }
public static void MainX() { Client client = new Client(); Person person = new Person(); Employee employee = new Employee(); Console.WriteLine(client is Person); Console.WriteLine(person is Person); Console.WriteLine(client is Client); Console.WriteLine(person is Client); Console.WriteLine(employee is Client); Person clientAsPerson = client; Console.WriteLine(clientAsPerson is Client); Client correctClient1 = clientAsPerson as Client; Client correctClient2 = (Client)clientAsPerson; Client incorrentClient1 = person as Client; // = null Client incorrectClient2 = (Client)person; //выкинет исключение, т.к. person не является Client //Client incorrectClient3 = (Client)employee; // синтаксически невозможно }