Question Get type name of inherited class

bondra

Well-known member
Joined
Oct 24, 2020
Messages
77
Programming Experience
Beginner
What is the best way to go in order to get the class name when creating an object?
This is what I've got but need help with syntax as well as what the best practises are.



Class: Animal:
class Animal
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string FavFood { get; set; }
    public string TypeOfAnimal {get; set;};  // Can the type name of the current class be set or specified in the property directly? Or is that totally wrong?

   Console.WriteLine("TypeOfAnimal + Name );
}

class Cat : Animal:
{
    public string AnimalType { get; set; } = "Cat"; // Or should it be set in the inherited class likeso?
}

class Application:
class Application
   public void CreateDummyAnimals()
   {
       Petowner.Pets.Add(new Cat
       {
           Name = "Missy",
           Age = 3,
           FavFood = "Mice",
           Breed = "Ragdoll",
           TypeOfAnimal = // How do I get the name of the Cat object?
           Toys = new List<Toy>()
    });
}
 
Last edited:
If you want the name of a type then you can get that from the type, e.g.
C#:
using System;
using System.Collections.Generic;

namespace Wunnell.ScratchPad.CS.NetFx.ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            var animals = new List<Animal>();

            animals.Add(new Cat());
            animals.Add(new Dog());

            foreach (var animal in animals)
            {
                Console.WriteLine(animal.GetType().Name);
            }

            Console.ReadLine();
        }
    }

    public class Animal
    {

    }

    public class Cat : Animal
    {

    }

    public class Dog : Animal
    {

    }
}
If you actually did want to build this into the types then you would not have the property set from outside when the object is created because then it could be set to anything. You would do it inside the types:
C#:
using System;
using System.Collections.Generic;

namespace Wunnell.ScratchPad.CS.NetFx.ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            var animals = new List<Animal>();

            animals.Add(new Cat());
            animals.Add(new Dog());

            foreach (var animal in animals)
            {
                Console.WriteLine(animal.TypeOfAnimal);
            }

            Console.ReadLine();
        }
    }

    public abstract class Animal
    {
        public abstract string TypeOfAnimal { get; }
    }

    public class Cat : Animal
    {
        public override string TypeOfAnimal => "Cat";
    }

    public class Dog : Animal
    {
        public override string TypeOfAnimal => "Dog";
    }
}
Now the property is read-only and must be implemented by every type that inherits Animal. The value is provided by the type itself, not the code that creates an instance of the type, so it is guaranteed to be consistent and correct.
 
You could also combine the codes posted by @jmcilhinney with a property in base (Animal) class that returns the type name:
C#:
public string TypeOfAnimal
{
    get { return this.GetType().Name; }
}
It will return for example "Cat" from an instance of Cat class.
Mark it virtual if you also want to option to override in inherited classes.
 
Back
Top Bottom