gpt4 book ai didi

c# - 为什么 var 在遍历 PropertyDescriptorCollection 时表现不同?

转载 作者:行者123 更新时间:2023-11-30 22:05:56 24 4
gpt4 key购买 nike

<分区>

在下面的代码中,在 foreach 循环中使用 var 会破坏代码。这是为什么?

using System;
using System.ComponentModel;

namespace ConsoleApplication1
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Guid Ssn { get; set; }
}

internal class Program
{
private static void Main(string[] args)
{
var foo = new Person {Name = "Foo", Age = 99, Ssn = Guid.NewGuid()};

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(foo);

foreach (PropertyDescriptor property in properties)
{
Console.WriteLine(property.Name);//Works
}

//foreach (var property in properties)
//{
// Console.WriteLine(property.Name);//Does not work
//}
}
}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com