Q:

C# program to check a class is a sub-class of a specified class or not

0

C# program to check a class is a sub-class of a specified class or not

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to check a class is a sub-class of a specified class or not is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to check a class is sub-class of the specified class.

using System;
using System.Reflection;

class ABC
{ 
    public ABC()
    {
        Console.WriteLine("ABC: Contructor called");
    }
}

class XYZ:ABC
{
    public XYZ()
    {
        Console.WriteLine("XYZ: Contructor called");
    }
}


class Program
{
    static void Main()
    {
        Type type1 = typeof(ABC);
        Type type2 = typeof(XYZ);

        if (type2.IsSubclassOf(type1) == true)
        {
            Console.WriteLine("XYZ class is sub class of ABC class");
        }
        else
        {
            Console.WriteLine("XYZ class is not sub class of ABC class");
        }
    }
}

Output:

XYZ class is sub class of ABC class
Press any key to continue . . .

Explanation:

In the above program, we created three classes ABCXYZ, and Program. Here, we inherited the ABC class into XYZ class.

The program class contains the Main() method. The Main() method is the entry point for the program. Here, we check a class is a subclass of a specified class using the IsSubclassOf() method of Type class and printed the appropriate message on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to check a specified type is an enum or... >>
<< C# program to check a specified class is a seriali...