0

Problem:

I have a method that uses generics and params to accept multiple IList parameters. However, when calling this method with multiple ObservableCollection parameters, one of the calls is not working as expected.

Code

public class Test
{
    public ObservableCollection<ModelDetail> Details { get; set; } = new();
    public ObservableCollection<ModelCostCenterDetail> CostCenterDetails { get; set; } = new();
    public ObservableCollection<ModelVoucherAdjustmentDetail> VoucherAdjustments { get; set; } = new();

    public Test()
    {
        // it is not working 
        //Main Problem is in this line
        **Details.DeleteRows2(CostCenterDetails,VoucherAdjustments);**     

        // it is   working 
        Details.DeleteRows2(CostCenterDetails);

        // it is also  working 
        Details.DeleteRows2(VoucherAdjustments);
    }
}

public class BaseModelTransactionDetails { }
public class BaseModelTransactionSubDetails { }
public class ModelDetail : BaseModelTransactionDetails { }


public class ModelCostCenterDetail: BaseModelTransactionSubDetails { }

public class ModelVoucherAdjustmentDetail
    : BaseModelTransactionSubDetails { }

public static class Test2
{
    public static void DeleteRows2<T1, T2>(
        this IList<T1> detailsCollection,
        params IList<T2>[] subDetailsCollections)
    where T1 : BaseModelTransactionDetails
    where T2 : BaseModelTransactionSubDetails { }
}

Issue

When I call Details.DeleteRows2 with multiple ObservableCollection parameters, the method fails to compile with error: The type arguments for method 'Test2.DeleteRows2<T1, T2>(IList, params IList[])' cannot be inferred from the usage. Try specifying the type arguments explicitly.

The calls with a single parameter work fine, but the method call with multiple parameters does not.

Questions:

  1. Why is the method call with multiple IList parameters not working?
  2. Are there any issues with the generic constraints or params usage in this method?
  3. How can I adjust the method or usage to resolve this issue?

Additional Information:

  1. The method works fine with individual IList parameters.
  2. The problem occurs only when multiple IList parameters are provided in a single method call.
17
  • Please provide the exact error message you get. Commented Aug 30, 2024 at 8:46
  • 5
    Well it can't be that. Neither ObservableCollection<ModelCostCenterDetail> nor ObservableCollection<ModelVoucherAdjustmentDetail> are convertible to that type. List<Child> is not List<Parent>. Commented Aug 30, 2024 at 9:13
  • 1
    ModelVoucherAdjustmentDetail is derived from BaseModelTransactionSubDetails, but IList<ModelVoucherAdjustmentDetail> is not derived from IList<BaseModelTransactionSubDetails>. Unless the type parameter is covariant, but IList<T> isn't. Commented Aug 30, 2024 at 10:09
  • 1
    Becasue a single parameter can be inferred as IList<ModelCostCenterDetail> or IList<ModelVoucherAdjustmentDetail>. Commented Aug 30, 2024 at 11:24
  • 1
    Does this work? Test2.DeleteRows2<ModelDetail,BaseModelTransactionSubDetails> .DeleteRows2( Details, CostCenterDetails, VoucherAdjustments ); Commented Aug 30, 2024 at 11:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.