Akshay Ghejji Akshay Ghejji -4 years ago 83
C# Question

Simple polymorphism in C#

This program is about polymorphism in C#. But call method does not working in visual studio. The program give error like "The name 'Call' does not exist in the current context".

class dog
{
}
class cat
{
}
class process
{
static void Call(dog d)
{
Console.WriteLine("dog is called");
}
static void Call(cat c)
{
Console.WriteLine("cat is called");
}
}
class polymorphism
{
public static void Main()
{
dog dog = new dog();
cat cat = new cat();
Call(dog);
}

}

Answer Source
class process
{
    public static void Call(dog d) //2
    {
        Console.WriteLine("dog is called");
    }
    public static void Call(cat c) //3
    {
        Console.WriteLine("cat is called");
    }
}
class polymorphism
{
    public static void Main()
    {
        dog dog = new dog();
        cat cat = new cat();
        process.Call(dog); //1
    }
}

What changes I've done?

  1. process has Call, not polymorphism. So, call Call by referring process.

  2. and 3. You cannot access a method outside class if you haven't defined an access level. So, I added public as we need to access Call outside the process class.

Hope it helped.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download