I have created a 2-dimensional list and want to return the maximum value, the minimum value, and the names that have the same index as these values.
This is what i did.
class Info
{
private string stad;
private double temperatur;
public string Stad
{
get { return stad; }
set { stad = value; }
}
public double Temperatur
{
get { return temperatur; }
set { temperatur = value; }
}
}
static double SearchMax(List<Info> list)
{
if (list.Count == 0)
{
throw new InvalidOperationException("Tomt Lista");
}
double Temperatur = double.MinValue;
foreach (Info temperatur in list)
{
if (temperatur.Temperatur > Temperatur)
{
Temperatur = temperatur.Temperatur;
}
}
return Temperatur;
}
static double SearchMin(List<Info> list)
{
if (list.Count == 0)
{
throw new InvalidOperationException("Tomt Lista");
}
double Tempertatur = double.MaxValue;
foreach (Info temperatur in list)
{
if (temperatur.Temperatur < Tempertatur)
{
Tempertatur = temperatur.Temperatur;
}
}
return Tempertatur;
}
But what I get in return is only the maximum and minimum values, not the name of the city that the temperature is in.
Can anyone tell me or show me how to return both?
Thank you for all the help you provide but it just put me back to where I was before posting this up here. What I want is for the program to return both the city and the temperature in the city
i.e “Warmest city is Oslo and it’s 20 °C warm”
and not “Warmest city is consoleaplication.info”
edit:2
Thank you for all yours help and especialy Alex ^_^
You can ammend your
Infoclass to overrideToStringwhich will return the formatted string when you call either of the functions above when a string is expected, argument pass, variable assignment etc.Sample implementation: