Welsing
Welsing

Reputation: 3

Storing structs in list and then displaying list?

So I'm completely new to programming; I've read the many similar questions and respective answers on here, and spent more time than I'd like to admit trying different ways of solving my problem, but I can't seem to find where the problem lies. Anyway, here's my code:

public struct City
{
    public string cityName { get; set; }
    public float cityTemp { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        var cityList = new List<City>();
        cityList.Add(new City
        {
            cityName = "Stockholm",
            cityTemp = 22.65f
        });
        Console.WriteLine("List: ");
        Console.WriteLine(cityList);
        Console.ReadKey();
    }
}

How do I make the list store my structs and how do I display the list properly? Edit: I know I need to use foreach, this code was just a barebones representation of my problem.

Upvotes: 0

Views: 69

Answers (3)

James
James

Reputation: 2201

You need to loop over your list of cities to display them:

Console.WriteLine("List: ");

foreach(var city in cityList)
{
    Console.WriteLine(city.cityName + " " + city.cityTemp); 
}

Upvotes: 0

Belogix
Belogix

Reputation: 8147

You want something like this:

public struct City
{
    public string cityName { get; set; }
    public float cityTemp { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        var cityList = new List<City>();
        cityList.Add(new City
        {
            cityName = "Stockholm",
            cityTemp = 22.65f
        });
        cityList.Add(new City
        {
            cityName = "London",
            cityTemp = 25.24f
        });         
        Console.WriteLine("List: ");
        foreach (var city in cityList)
        {
            Console.WriteLine(string.Format("City: {0} is currently: {1}oC", city.cityName, city.cityTemp);
        }
        Console.ReadKey();
    }
}

You could also make your city output a sensible response by overriding ToString() like this:

public struct City
{
    public string cityName { get; set; }
    public float cityTemp { get; set; }
    public override string ToString()
    {
        return String.Format("City: {0} is currently: {1}oC", cityName, cityTemp);
    }
}

So then you could have:

class Program
{
    static void Main(string[] args)
    {
        var cityList = new List<City>();
        cityList.Add(new City
        {
            cityName = "Stockholm",
            cityTemp = 22.65f
        });
        cityList.Add(new City
        {
            cityName = "London",
            cityTemp = 25.24f
        });         
        Console.WriteLine("List: ");
        foreach (var city in cityList)
        {
            Console.WriteLine(city);
        }
        Console.ReadKey();
    }
}

Upvotes: 2

John Saunders
John Saunders

Reputation: 161773

The list is storing your structs. As for displaying them, that doesn't work by magic. You'll have to loop over the list and display each one.

public struct City
{
    public string cityName { get; set; }
    public float cityTemp { get; set; }
    public override string ToString()
    {
        return String.Format("{0} {1}", cityName, cityTemp);
    }
}

public void DisplayAll(IEnumerable<City> cities)
{
    foreach (var city in cities)
        Console.WriteLine(city);
}

Upvotes: 1

Related Questions