c# 自动实现属性 隐式类型 对象及集合初始化 匿名类型

Demo 

using System;
using System.Collections.Generic;

namespace IntelligentCompiling
{
    class Program
    {
        static void Main(string[] args)
        {
            //隐式类型数组
            var array =new int[12, 23, 32];
            //隐式类型集合
            var list = new List<int>();
            //隐式类型变量
            var intVar = 3;

            //无需构造函数的初始化
            Person p = new Person { Name = "Daniel", Weight = 50 };

            //集合初始化
            var names = new List<string>()
            {
                "Alen","Bill","Carl","Daniel"
            };

            //匿名类型   = 隐式类型+对象初始化
            //匿名类型对象
            var people = new { Name = "Alen", Age = 15 };
            Console.WriteLine("{0}的年龄为{1}", people.Name, people.Age);
            //匿名类型数组
            var personCollection = new[]
            {
                new {Name="Tom",Age=20},
                new {Name="Ellen",Age=21},
                new {Name="Frank",Age=22}
            };
            int totalAge = 0;
            foreach(var per in personCollection)
            {
                totalAge += per.Age;
            }
            Console.WriteLine("Age total:" + totalAge);
            Console.Read();
        }
    }

    class Person
    {
        //定义可读写属性
        public string Name { get; set; }
        //定义只读属性
        public int Age { get;private set; }
        public int Weight { get; set; }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值