이 가능한 중복 : 나는 오히려에 놀라게했다
Why does var evaluate to System.Object in “foreach (var row in table.Rows)”?"VAR"형식 유추는
는 ....
SqlDataReader reader = cmd.ExecuteReader();
DataTable schemaTable = reader.GetSchemaTable();
// the following compiles correctly
foreach (DataRow field in schemaTable.Rows)
{
Console.WriteLine(field["ColumnName"]);
}
// the following does not compile as 'var' is of type 'object'
foreach (var field in schemaTable.Rows)
{
// Error: Cannot apply indexing with [] to an expression of type 'object'
Console.WriteLine(field["ColumnName"]);
}
가는 무슨 다음 오늘 발견 여기에?
이것은 유형 유추 실패입니까? 그렇다면 무엇이 그것을 일으키는가?
정의 된 동작 또는 var
의 일부입니까? 그렇다면 왜?
var
의 아이디어는 당신이 행동을 변경하지 않고 변수 선언/초기화
어디에서나 사용할 수 있다고 생각했다.
'DataRowCollection.GetEnumerator()'는 'IEnumerator'를 반환합니다. 'IEnumerator.Current'의 타입은'object'입니다. –
[정확한 중복 질문] (http://stackoverflow.com/questions/2325777/why-cant-i-do-foreach-var-item-in-datatable-rows) 기본적으로 행'DataRowCollection', 그래서 컴파일러는 'DataRow'가 아닌'Object'를 선택합니다. – Axarydax