IMMORTAL
IMMORTAL

Reputation: 2857

Unable to cast object Type Error

Unable to cast object Type Error

Code

class Person
    {
        public int Addition(int a, int b)
        {
            return a + b;
        }
    }
    class Developer : Person
    {
        public void Addition(int a, int b)
        {
            int sum = a + b;
            Console.WriteLine("Total is {0}{1}" + sum);
        }
    }
    class CHashDeveloper : Developer
    {


        static void Main(string[] args)
        {
            Developer objDeveloper =(Developer) new Person(); // Error Occurred here
        }

what I need to do so that I can resolve such error. }

Upvotes: 0

Views: 462

Answers (1)

SLaks
SLaks

Reputation: 888233

A Person is not a Developer.
You can only cast an object to a type that the object actually is.

If you want to create a Developer, you need to write that: new Developer().

Upvotes: 1

Related Questions