0

Please find the below code. Can any one tell me how Activator.CreateInstance Working with abstract class? I have created abstract class, in Abstract class have one abstract method and inheritted in normal three different classes. The class which I mentioned it is returned correct value, I want to know how its working? Thanks Advance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sample_App
{
    abstract class ABSClass
    {
        public abstract int AddTwoNumbers();

    }
class FirstClass : ABSClass
{
    public override int AddTwoNumbers()
    {
        return 1 + 2;
    }
}

class SecondClass : ABSClass
{
    public override int AddTwoNumbers()
    {
        return 3 + 2;
    }
}

class ThirdClass : ABSClass
{
    public override int AddTwoNumbers()
    {
        return 3 + 2;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Type ClassType = Type.GetType("Sample_App.FirstClass");
        ABSClass obj = Activator.CreateInstance(ClassType) as ABSClass;
        int i = obj.AddTwoNumbers();
    }


}

}

Rajkumar
  • 11
  • 8
  • `Activator.CreateInstance` has absolutely nothing to do with `ABSClass` in this example. You are just casting the return value, which of course you are free to do. – Jon Oct 07 '13 at 10:19
  • Thank you @Jon. I understood the program. I want to know wats the role of Activator.CreateInstance? At When we have to use this method. We have implemented abstract method in multiple classes in program, how it goes correctly to specified class? then Activator.Creatinstance Method will work normal class? – Rajkumar Oct 07 '13 at 10:27
  • See http://stackoverflow.com/questions/7598088/purpose-of-activator-createinstance-with-example – Jon Oct 07 '13 at 10:34

0 Answers0