简体   繁体   中英

how to make generic method having List<dto> paramter for method c#

Need to create generic method which will accept List<child1> and List<child2> .

public class Parent
{
    public string Name { get; set; }
    public decimal ProductRate { get; set; }
}

public class Child1 : Parent
{

}

public class Child2 : Parent
{

}

Program : As shown below i am trying to to pass the values in method

public class HelloFriend
{
    public static void Main(string[] args)
    {
        List<Child1> child1 = new List<Child1>()
        {
            new Child1(){ Name="Admin", ProductRate = 10}
        };

        List<Child2> child2 = new List<Child2>()
        {
            new Child2(){ Name="Admin", ProductRate = 50}
        };

        decimal result1 =  getvalues(child1, 10);
        decimal result2 =  getvalues(child2, 10);
    }

    public static decimal getvalues(List<Child1> list, decimal calculateValue)
    {
        decimal value = 1;

        if(parentList !=null )
        {
            foreach( var item in parentList)
            {
                value = item.ProductRate * 100 * calculateValue;
            }
        }
        return value;
    }
}

How to make getvalues() generics that will work with all List of Child1 and Chil2

Basic knowledge about inheritance: use a list of Parent and declare the getters/setters you need in Parent (like you did).

public static decimal getvalues(List<Parent> list, decimal calculateValue)
{

}

As the comments said, usage: (use Parent as list type)

List<Parent> child1 = new List<Parent>()
{
    new Child1(){ Name="Admin", ProductRate = 10}
};
decimal result1 =  getvalues(child1, 10);

Alternative: (cast the child-list)

List<Child1> child1 = new List<Child1>()
{
    new Child1(){ Name="Admin", ProductRate = 10}
};
decimal result1 =  getvalues(child1.Cast<Parent>(), 10);

A short console project to illustrate the use of generic types here: [I used the object definitions of your question]

static void Main()
{
    List<Child1> child1s = new List<Child1>()
    {
        new Child1() { Name="c11", ProductRate=1},
        new Child1() { Name="c12", ProductRate=2}
    };

    List<Child2> child2s = new List<Child2>()
    {
        new Child2() { Name="c21", ProductRate=30},
        new Child2() { Name="c21", ProductRate=60}
    };

    foreach (var retval in GetValues(child1s, 5))
        System.Console.WriteLine(retval);
    foreach (var retval in GetValues(child2s, 5))
        System.Console.WriteLine(retval);

    System.Console.ReadKey();
}
public static IEnumerable<decimal> GetValues<T>(List<T> items, decimal calculatedValue) where T : Parent
{
    foreach (var item in items)
    {
        yield return (decimal)(item.ProductRate * 100 * calculatedValue);
    }
}

The function is defined as List<T> where T is the generic type parameter. This parameter is further limited by where T : Parent to fit only on objects of type Parent or inherited types of it.

You can also get the type of the given instance by typeof(T) to differentiate if needed, but for this kind you should first read further into generics.

Another way is as in KYL3Rs answer described, to define the input parameter as IEnumerable<Parent> (in his answer List<Parent> ). That way you need no generics, just inheritance and implicit casting. You need IEnumerable<T> here, otherwise a conversation isn't implicit and must be made by hand.

static void Main()    
{
    .....
    foreach (var retval in GetValues(child1s, 5))
        System.Console.WriteLine(retval);
    foreach (var retval in GetValues(child2s, 5))
        System.Console.WriteLine(retval);

    System.Console.ReadKey();
}
public static IEnumerable<decimal> GetValues(IEnumerable<Parent> items, decimal calculatedValue)
{
    foreach (var item in items)
    {
        yield return (decimal)(item.ProductRate * 100 * calculatedValue);
    }
}

Please also note my return value of a list of items ( IEnumerable<decimal> ) and the yield return statement. I think your single return value after processing a list was a bug. And I use IEnumerable to make it clear I do not modify the given collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM