Wednesday, March 20, 2013

The C# IS operator

will return true for tests of Interface implementation and class parentage


    interface IFuddy
    {
       string  test();
    }

    public class testBase: IFuddy
    {
        public string test()
        {
            return "hody";
        }
    }


    public class testA : testBase
    {
        public string atest()
        {
            return "hodydoe";
        }
    }

    public class testB : testBase
    {
        public string btest()
        {
            return "awaywego";
        }
    }


   private void retu(IFuddy f)
        {
            if (f is testA)
            {
                Response.Write((f as testA).atest());
            }

            if (f is testB)
            {
                Response.Write((f as testB).btest());
            }
            if (f is testBase)
            {
                Response.Write("f is testBase");
            }
            if (f is IFuddy)
            {
                Response.Write("f is IFuddy");
            }


        }

      protected void Button12_Click(object sender, EventArgs e)
        {
            testA t1 = new testA();
            testB t2 = new testB();
            retu(t1);
            retu(t2);
        }

Will write:

hodydoe
f is testBase
f is IFuddy
awaywego
f is testBase
f is IFuddy

No comments:

Post a Comment