Csharp/C Sharp/Language Basics/is
Содержание
choose between two overloaded methods at run-time using the "is" keyword
using System;
public class BankAccount {
virtual public void Withdraw() {
Console.WriteLine("Call to BankAccount.Withdraw()");
}
}
public class SavingsAccount : BankAccount {
override public void Withdraw() {
Console.WriteLine("Call to SavingsAccount.Withdraw()");
}
}
public class MainClass {
public static void Main(string[] strings) {
BankAccount ba = new BankAccount();
Test(ba);
SavingsAccount sa = new SavingsAccount();
Test(sa);
}
public static void Test(BankAccount baArgument) {
if (baArgument is SavingsAccount) {
SavingsAccount saArgument = (SavingsAccount)baArgument;
saArgument.Withdraw();
} else {
baArgument.Withdraw();
}
}
}
is Checker
using System;
using System.Collections.Generic;
using System.Text;
class Checker {
public void Check(object param1) {
if (param1 is ClassA)
Console.WriteLine("Variable can be converted to ClassA.");
else
Console.WriteLine("Variable can"t be converted to ClassA.");
if (param1 is IMyInterface)
Console.WriteLine("Variable can be converted to IMyInterface.");
else
Console.WriteLine("Variable can"t be converted to IMyInterface.");
if (param1 is MyStruct)
Console.WriteLine("Variable can be converted to MyStruct.");
else
Console.WriteLine("Variable can"t be converted to MyStruct.");
}
}
interface IMyInterface {
}
class ClassA : IMyInterface {
}
class ClassB : IMyInterface {
}
class ClassC {
}
class ClassD : ClassA {
}
struct MyStruct : IMyInterface {
}
class Program {
static void Main(string[] args) {
Checker check = new Checker();
ClassA try1 = new ClassA();
ClassB try2 = new ClassB();
ClassC try3 = new ClassC();
ClassD try4 = new ClassD();
MyStruct try5 = new MyStruct();
object try6 = try5;
Console.WriteLine("Analyzing ClassA type variable:");
check.Check(try1);
Console.WriteLine("\nAnalyzing ClassB type variable:");
check.Check(try2);
Console.WriteLine("\nAnalyzing ClassC type variable:");
check.Check(try3);
Console.WriteLine("\nAnalyzing ClassD type variable:");
check.Check(try4);
Console.WriteLine("\nAnalyzing MyStruct type variable:");
check.Check(try5);
Console.WriteLine("\nAnalyzing boxed MyStruct type variable:");
check.Check(try6);
Console.ReadKey();
}
}
The is operator confirms that the employee is a manager.
using System;
public class Starter {
public static void Main() {
Manager person = new Manager("Accounting");
Console.WriteLine("[Menu]\n");
Console.WriteLine("Task 1");
Console.WriteLine("Task 2");
if (person is IManager) {
IManager mgr = person;
Console.WriteLine("\n[{0} Menu]\n",mgr.Department);
Console.WriteLine("Task 3");
}
}
}
public interface IManager {
string Department {
get;
}
}
public class Employee {
}
public class SalariedEmployee : Employee {
}
public class Manager : SalariedEmployee, IManager {
public Manager(string dept) {
propDepartment = dept;
}
private string propDepartment;
public string Department {
get {
return propDepartment;
}
}
}
Using the is Keyword to Work with an Interface
using System;
public interface IPrintMessage
{
void Print();
};
class Class1
{
public void Print()
{
Console.WriteLine("Hello from Class1!");
}
}
class Class2 : IPrintMessage
{
public void Print()
{
Console.WriteLine("Hello from Class2!");
}
}
class MainClass
{
public static void Main()
{
PrintClass PrintObject = new PrintClass();
PrintObject.PrintMessages();
}
}
class PrintClass
{
public void PrintMessages()
{
Class1 Object1 = new Class1();
Class2 Object2 = new Class2();
PrintMessageFromObject(Object1);
PrintMessageFromObject(Object2);
}
private void PrintMessageFromObject(object obj)
{
if(obj is IPrintMessage)
{
IPrintMessage PrintMessage;
PrintMessage = (IPrintMessage)obj;
PrintMessage.Print();
}
}
}