create a method with two lists, iterate on both and add to another list in csharp

You can create a method that takes two lists as its parameters, iterates over both lists, and adds the elements from both lists to a new list. Here's an example of what the method would look like:

main.cs
public List<int> AddLists(List<int> list1, List<int> list2)
{
    List<int> resultList = new List<int>();
    for (int i = 0; i < list1.Count && i < list2.Count; i++)
    {
        resultList.Add(list1[i] + list2[i]);
    }
    return resultList;
}
249 chars
10 lines

In this example, the method takes two lists of integers as its parameters: list1 and list2. A new list called resultList is created to hold the sum of the elements from list1 and list2.

The for loop iterates over both lists, adding the elements at the same index in each list and storing the result in the resultList using the Add method.

Finally, the method returns the resultList with the summed elements of both lists.

gistlibby LogSnag