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();
}
}
}
}
}
변수에 'squiggley'가 지정됩니다. 'item'변수에만 할당됩니다. 이 코드는 Paul Kimmel의 책 C# Unleashed from Sams에서 약간 수정되었습니다.익명 형식의 foreach
var quotes = new object[] { quote1, quote2, quote3 };
따옴표는 객체 배열이 아닌 익명 형식의 배열입니다 :
어떨까요? – zerkms
foreach 루프에서 코드를 생략 했습니까? –
그래, 당신은 iterating하고 있지만 항목으로 아무것도하지 마십시오. 따라서 할당 된 것입니다. –