Foreach over Anonymous Types

using System;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace LearningJustCode
{
    class Program
    {
        static void Main(string[] args)
        {
            Update();
        }

        static void Update()
        {
            var quote1 = new { Stock = "DELL", Quote = GetQuote("DELL") };
            var quote2 = new { Stock = "MSFT", Quote = GetQuote("MSFT") };
            var quote3 = new { Stock = "GOOG", Quote = GetQuote("GOOG") };

            var quotes = new object[] { quote1, quote2, quote3 };

            foreach (var item in quotes)
            {
                Console.WriteLine(item.Stock);
                Console.WriteLine(item.Quote.ToString());
            }
        Console.ReadKey();

        }

        static string GetQuote(string stock)
        {
            try
            {
                return InnerGetQuote(stock);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
                return "N/A";
            }
        }

        static string InnerGetQuote(string stock)
        {
            string url = @"http://www.webservicex.net/stockquote.asmx/GetQuote?symbol={0}";
            var request = HttpWebRequest.Create(string.Format(url, stock));

            using (var response = request.GetResponse())
            {
                using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
                {
                    return reader.ReadToEnd();
                }
            }


        }
    }
}

I get squiggley over the item, the value for the variable "item" is assigned. This code has been slightly modified from Paul Kimmel's C # Unleashed book from Sams.

+3
source share
2 answers

Your array should be of the type of your stock quote. Your stock quote is an anonymous type, so we also need to define the array anonymously. This can be done cleanly:

    var quotes = new[]{  new { Stock = "DELL", Quote = "123" },
                         new { Stock = "MSFT", Quote = "123" },
                         new { Stock = "GOOG", Quote = "123" }};

    foreach (var item in quotes)
    {
        Console.WriteLine(item.Stock);
        Console.WriteLine(item.Quote.ToString());
    }
+3
source

I think the problem is in this line:

var quotes = new object[] { quote1, quote2, quote3 };

quotes is an array of objects, not an array of this anonymous type. foreach also only has an object value. you can try to form an array in one line or in a lambda expression

A "var" "dynamic"

        var quote1 = new { Stock = "DELL", Quote = ("DELL") };
        var quote2 = new { Stock = "MSFT", Quote = ("MSFT") };
        var quote3 = new { Stock = "GOOG", Quote = ("GOOG") };

        var quotes = new object[] { quote1, quote2, quote3 };

        foreach (dynamic item in quotes)
        {
            var r = item.Stock;

        }

"",

        var quote1 = new { Stock = "DELL", Quote = ("DELL") };
        var quote2 = new { Stock = "MSFT", Quote = ("MSFT") };
        var quote3 = new { Stock = "GOOG", Quote = ("GOOG") };

        var quotes = new [] { quote1, quote2, quote3 };

        foreach (var item in quotes)
        {
            var r = item.Stock;

        }
+4

All Articles