C# 3.0 新特性之隐含类型局部变量

C# 3.0 新特性之隐含类型局部变量
2009年8月18日 云飞扬 发表评论 阅读评论
-
C# 3.0 新特性隐含类型局部变量(Local Variable Type Inference)

一般情况下我们可以定义如下变量
    int i = 43;

    string s = "...This is only a test...";

    int[] numbers = new int[] { 4, 9, 16};

    SortedDictionary<string, List<DateTime>> complex =
        new SortedDictionary<string, List<DateTime>>();
==================================================================
var 可变变量的定义则如下定义:
            var i = 43;

            var s = "...This is only a test...";

            var numbers = new[] { 4, 9, 16 };

            var complex = new SortedDictionary<string, List<DateTime>>();
这些变量在调试的时候可以把鼠标放到var上,则var则显示定义的类型
==================================================================
var 隐含类型数组如何正确定义
错误的定义如下
var x;// 错误,因为这样不知道什么类型
    x = new int[] { 1, 2, 3 };

var x = {1, 2, 3}; // var数组这样的定义是不允许的

var x = new[] {1, 2, 3};//正确定义

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NewLanguageFeatures
{
    public class Customer
    {
        public int CustomerId { get; private set; }

        public string Name { get; set; }
        public string City { get; set; }

        public Customer(int Id)
        {
            CustomerId = Id;
        }

        public override string ToString()
        {
            return Name + "/t" + City + "/t" + CustomerId;
        }
    }

    class Program
    {
        static void VarTest()
        {
            var x = new[] { 1, 2, 3 };//var数组的定义
        }

        static void Main(string[] args)
        {
            var customers = CreateCustomers();//var 方法定义

            Console.WriteLine("Customers:/n");
            foreach (var c in customers)//var 参数定义
                Console.WriteLine(c);
        }

        static List<Customer> CreateCustomers()
        {
            return new List<Customer>
                {
                    new Customer(1) { Name = "Alex Roland",             City = "Berlin"    },
                    new Customer(2) { Name = "Oliver Cox",              City = "Marseille" },
                    new Customer(3) { Name = "Maurice Taylor",          City = "London"    },
                    new Customer(4) { Name = "Phil Gibbins",            City = "London"    },
                    new Customer(5) { Name = "Tony Madigan",            City = "Torino"    },
                    new Customer(6) { Name = "Elizabeth A. Andersen",   City = "Portland"  },
                    new Customer(7) { Name = "Justin Thorp",            City = "London"    },
                    new Customer(8) { Name = "Bryn Paul Dunton",        City = "Portland"  }
                };
        }     
    }
}


本文来自: 本站内容欢迎转载,但是禁止去掉本文链接(转载无妨,去掉链接可耻!):http://www.ajaxcn.net/archives/178

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值