AnonymousTypes
{
    //AutomaticProperties(自动属性)
    public int ID { getset; }
    
public string Name { getset; }
    
public int Age { getset; }

    
public void AnonymousTypesTest()
    {
        
//CollectionInitializers(集合初始化器)
        List<AnonymousTypes> list = new List<AnonymousTypes>
        {
            
//ObjectInitializers(对象初始化器)
            new AnonymousTypes { ID = 1, Name = "webabcd", Age = 10 },
            
new AnonymousTypes { ID = 2, Name = "webabcdefg", Age = 20 },
            
new AnonymousTypes { ID = 3, Name = "webabcdefghijklmn", Age = 30 }
        };

        
// listAnonymousTypes - 匿名类型
        var listAnonymousTypes = from l in list
                                 
where l.Name == "webabcd"
                                 select 
new { Name = l.Name, Age = l.Age };

        
foreach (var v in listAnonymousTypes)
        {
            
// v - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense
            string name = v.Name;
            
int age = v.Age;
        }

        
// 声明匿名类型:将new关键词后面的类型名称省略掉
        var person = new { Name = "webabcd", Age = 27 };
        
// person - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense
        string myName = person.Name;
        
int myAge = person.Age;
    }
}

相关文章:

  • 2022-03-11
  • 2021-11-23
  • 2021-12-24
  • 2022-01-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-10
  • 2022-01-18
  • 2021-09-19
  • 2022-12-23
相关资源
相似解决方案