skip to Main Content

The blow code is not running giving the error: "Error CS1503: Argument 1: cannot convert from ‘Child1’ to ‘Test<bool, object>’", I am creating the abstract class, which takes the Same abstract class as a parameter in the constructor. and After creating the child class of them and passing another child class as a parameter to one’s constructor giving compilation error as mentioned.
Tried ChatGPT it says logically code is correct, but why visual studio code is giving compile time error.
https://learn.microsoft.com/en-us/answers/questions/1379331/compilation-error-cs8625-c

abstract class Test<A, B>
{
    private Test<B, object> t1;

    public Test(Test<B, object> t)
    {
        t1 = t;
    }
}

class Child1 : Test<int, string>
{
    public Child1(Test<string, object> t) : base(t)
    {
    }
}

class Child2 : Test<string, bool>
{
    public Child2(Test<bool, object> t) : base(t)
    {
    }
}

class Main
{
    public void Init()
    {
        Child2 c2 = new Child2(null);
        Child1 c1 = new Child1(c2);
    }
}

2

Answers


  1. That error looks right to me.

    • t1 is a Child1, which is a Test<string, string>.
    • t2 is a Child2.
    • Child2‘s constructor takes a Test<bool, object>
    • t1 cannot be converted from a Test<string, string> to a Test<bool, object> because its specified class-types don’t match. Simply, a string is not a bool.

    Maybe, you want the Child1 and Child2 sub-classes to be generic too.

    Login or Signup to reply.
  2. I reproduce the code in my local like below.

    enter image description here

    Here is the working sample, it can help you fix it.

    namespace CS1503
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello, World!");
                Child2 c2 = new Child2(null); 
                Child1 c1 = new Child1(c2);
            }
        }
        interface ITest<out A, out B>
        {
            // Covariant interface with no members that use A or B in input positions.
        }
    
        abstract class Test<A, B> : ITest<A, B>
        {
            private ITest<B, object> t1;
    
            protected Test(ITest<B, object> t)
            {
                t1 = t;
            }
    
            // Additional functionality for the Test class could be added here.
        }
    
        class Child1 : Test<int, string>
        {
            public Child1(ITest<string, object> t) : base(t)
            {
            }
    
            // Additional functionality for the Child1 class could be added here.
        }
    
        class Child2 : Test<string, bool>, ITest<string, object> // Explicitly implementing the needed interface
        {
            public Child2(ITest<bool, object> t) : base(t)
            {
            }
    
            // Additional functionality for the Child2 class could be added here.
        }
    
    }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search