"I have 3 list strings. 2 list strings contains double values."
If your lists are
lists of strings where
each string is a representation of a double value: you
must convert the strings to doubles first, then do the subtraction, and then convert the result to a string, and then assign that to your new List of strings !
"Subtracting" a string from a string makes no sense (and wouldn't compile in the first place) ... in C#, anyhow.
Here's a code example that takes two Lists of strings, where each item in each list is assumed to be a string representation of a double, subtracts the items in one list from the other list, takes the resulting double and converts into a string, and stores it in the list it returns.
Note this example tests to make sure that the for-loop attempts to compare the two lists using the shorter of the two lists ... if the two lists are not of the same length.
It also checks each item in each list to make sure they are doubles:
private List<string> subtractStringDouble(List<string> list1, List<string> list2)
{
List<string> list3 = new List<string>();
double d1, d2;
// use the shorter of the two lists
// you'll note that you can evaluate inside
// a for-loop declaration
for (int i = 0; i < ((list1.Count < list2.Count) ? list1.Count : list2.Count); i++)
{
if(double.TryParse(list1[i], out d1))
{
if(double.TryParse(list2[i], out d2))
{
list3.Add((d1 - d2).ToString(format:"F"));
}
}
}
return list3;
}
Here's a test:
List<string> test1 = new List<string>{ "5.33", "59.123", "1.9"};
List<string> test2 = new List<string> { "2.33", "56.123", "-1.1" };
List<string> test3 = subtractStringDouble(test1, test2);
Each of the three items in 'list3 should be: "3.00"