Example #1
0
        public void UnitRb_CtorSubclass()
        {
            var  sub  = new DerivedB();
            bool isRO = ((System.Collections.Generic.ICollection <int>)sub).IsReadOnly;

            Assert.IsFalse(isRO);
        }
        public void test_collectionExtensions_listToString_source()
        {
            string output = string.Empty;

            List <Base> list = null;

            output = list.listToString();

            Assert.AreEqual(output, "null");

            list = new List <Base>();

            output = list.listToString();

            Assert.AreEqual(output, "[]");

            DerivedA obj_a = new DerivedA();

            list.Add(obj_a);

            output = list.listToString();

            Assert.AreEqual(output, "[" + obj_a + "]");

            DerivedB obj_b = new DerivedB();

            list.Add(obj_b);

            output = list.listToString();

            Assert.AreEqual(output, "[" + obj_a + ", " + obj_b + "]");
        }
Example #3
0
        // Ex 2: Explicit conversion
        public void Ex2_Explicit()
        {
            double d = 10.5;
            int    i = (int)d; // cast required (truncation)

            BaseA    ba = new DerivedB();
            DerivedB db = (DerivedB)ba;
        }
Example #4
0
    public static MyBase CreateDerivedB()
    {
        string specialPropertyOfB =      // ...
                                    MyBase instance = new DerivedB(specialPropertyOfA);

        Recorder.Process(instance);
        return(instance);
    }
Example #5
0
        public void Ex1_Implicit()
        {
            int    i = 7;
            double d = i; // allowed

            DerivedB db = new DerivedB();
            BaseA    ba = db; // allowed (polymorphism)

            BaseA  ba2 = new BaseA();
            IBase0 ib0 = ba2; // allowed (interface polymorphism)
        }
Example #6
0
    public void TestMethod1()
    {
        IList <DerivedA> lista = new List <DerivedA> {
            new DerivedA()
        };
        var casted = lista.Cast <DerivedB>();

        try {
            DerivedB b = casted.First();
            Assert.Fail();
        } catch (InvalidCastException) {
            // exception will be thrown
        }
    }
Example #7
0
    private static void Main(string[] args)
    {
        Base     baseObj = new Base();
        Base     ba      = new DerivedA();
        Base     bb      = new DerivedB();
        DerivedB b       = new DerivedB();

        baseObj.SomeMethod();
        ba.SomeMethod();
        bb.SomeMethod();
        b.SomeMethod();

        Console.ReadKey();
    }
        public int doAnything(int x)
        {
            DerivedA a = this as DerivedA;

            if (a != null)
            {
                return(a.doSomething(x));
            }

            DerivedB b = this as DerivedB;

            if (b != null)
            {
                return(b.doSomethingElse(x));
            }

            return(0);
        }
 protected internal virtual string Generate(DerivedB b)
 {
     return(" B ");
 }