Pankaj Sathe
Pankaj Sathe

Reputation: 21

Implicit typecasting not working

I have an abstract class called Product and another class called DifferentProduct and an interface called IProduct.

Both class Product and class DifferentProduct are derived from an interface called IProduct.

public class Product : IProduct
{
    public int ID { get; set; }
    public string ProductName { get; set; }
}

public class DifferentProduct : IProduct
{
    public int ID { get; set; }
}


public interface IProduct
{
    int ID { get; set; }
}

I have function in which I am passing

    ProductListing( List<IProduct>, List<IProduct> )
    {

    }

Now trying to call function from some file

    List<Product> productList1;
    List<DifferentProduct> differentProductList;

    XYZ.ProductListing( productList1, differentProductList );

I am getting following errors on above line

error CS1503 : Argument 1 cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List '

error CS1503 : Argument 2 cannot convert from System.Collections.Generic.List to System.Collections.Generic.List

Is there any way to work out this solution without explicit typecasting ? I need to work without explicit typecasting.

Upvotes: 0

Views: 99

Answers (3)

MSE
MSE

Reputation: 345

Try Using generics :

class Program
{
    static void Main(string[] args)
    {
        test t = new test();
        List<Product> productList1 = new List<Product>();
        List<DifferentProduct> differentProductList = new List<DifferentProduct>();

        t.ProductListing(productList1, differentProductList);
    }
}

public class test
{
    public void ProductListing<T, U>(List<T> list1, List<U> list2)
        where T : IProduct
        where U : IProduct
    {

    }
}

public abstract class Product : IProduct
{
  public int ID { get; set; }
  public string ProductName{ get; set; }
}

public class DifferentProduct : IProduct
{
  public int ID{get; set;}
}


public interface IProduct
{
    int ID { get; set; }
}

Upvotes: 0

Arturo Menchaca
Arturo Menchaca

Reputation: 15982

That is because an List<Product> is not a List<IProduct>, the same with List<DifferentProduct>.

If parameter types in ProductListing(...) can be changed to a covariant interface like IEnumerable<IProduct> or IReadOnlyList<IProduct> then you can pass lists of Product or DifferentProduct without problems.

Something like this:

void ProductListing(IReadOnlyList<IProduct> list1, IReadOnlyList<IProduct> list2)
{
    ...
}

Then this is possible:

List<Product> productList1;
List<DifferentProduct> differentProductList;

XYZ.ProductListing(productList1, differentProductList);

Upvotes: 1

devlife
devlife

Reputation: 16145

Try this:

var productList1 = new List<IProduct>();
productList1.Add(new Product());

var differentProductList = new List<IProduct>();
differentProductList.Add(new DifferentProduct)();

XYZ.ProductListing(productList1, differentProductList);

You could also do something like this:

ProductListing<T, U>(List<T> list1, List<U> list2) where T : IProduct, U : IProduct

Upvotes: 2

Related Questions