[笔记] C# 3.0 新特性[1]-implicitly typed local variables

面对着观念的快速更新,一片茫然,幸得cnblogs这块宝地,让我有跟上时代的机会。我也应该开始做一下记录了,记点有用的东西,希望多多支持.

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Linq;
None.gif
using  System.Text;
None.gif
None.gif
namespace  Net30NewFeature1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class ImplicitlyTypedLocalVariableTest
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static void Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Test4();           
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 在.net 2.0 年代变量的声明是按预定的方式,就如同本函数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void Test1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int myInt = 0;
InBlock.gif            
bool myBool = true;
InBlock.gif            
string myString = "Time, marches ondot.gif";
InBlock.gif            
// Print out the underlying type.
InBlock.gif
            Console.WriteLine("myInt is a: {0}", myInt.GetType().Name);
InBlock.gif            Console.WriteLine(
"myBool is a: {0}", myBool.GetType().Name);
InBlock.gif            Console.WriteLine(
"myString is a: {0}", myString.GetType().Name);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 在.net 3.0 年代提供了一个 “var” 关键字,它可以用来替换一个特定的类型(such as int, bool, or string),
InBlock.gif        
/// 当你这样做时,编译器会根据“初始化的数据值”自动地推断出底层的数据类型。通过运行如下代码,你可以发现
InBlock.gif        
/// 实际上在运行中的implicitly typed local variables,
InBlock.gif        
/// 你也可以通过 Relfector 或 ildasm 来查看生成的assembly代码
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void Test2()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"***** Fun with Implicit Typing *****\n");
InBlock.gif            
// Implicitly typed local variables.
InBlock.gif
            var myInt = 0;
InBlock.gif            var myBool 
= true;
InBlock.gif            var myString 
= "Time, marches ondot.gif";
InBlock.gif            
// Print out the underlying type.
InBlock.gif
            Console.WriteLine("myInt is a: {0}", myInt.GetType().Name);
InBlock.gif            Console.WriteLine(
"myBool is a: {0}", myBool.GetType().Name);
InBlock.gif            Console.WriteLine(
"myString is a: {0}", myString.GetType().Name);            
ExpandedSubBlockEnd.gif        }

InBlock.gif       
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 你也需要了解,可以应用“隐式类型”的类型可以为 : arrays, generics, and custom class types:
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void Test3()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            var evenNumbers 
= new int[] dot.gif2468 };
InBlock.gif            var myMinivans 
= new List<string>();
InBlock.gif            var myCar 
= new System.IO.FileInfo("");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 可以在foreach循环的构造器中使用var关键字,编译器会自动识别正确的类型,
InBlock.gif        
/// 对于识别正确的类型,你需要学些关于推断规则,好像是与编译原理有很大的关系,
InBlock.gif        
/// 现在知道了形式语言的重要了。呵呵        
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void Test4()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Use 'var' in a standard for each loop.
ExpandedSubBlockStart.gifContractedSubBlock.gif
            var evenNumbers = new int[] dot.gif2468 };
InBlock.gif            
// Here, var is really a System.Int32.
InBlock.gif
            foreach (var item in evenNumbers)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"Item value: {0}", item);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// Use 'var' to declare the array of data.
ExpandedSubBlockStart.gifContractedSubBlock.gif
            var evenNumbers1 = new int[] dot.gif2468 };
InBlock.gif            
// Use a strongly typed System.Int32 to iterate over contents.
InBlock.gif
            foreach (int item in evenNumbers1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"Item value: {0}", item);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 特性: implicitly typed local variables,
InBlock.gif        
/// 你要注意到它的限制,
InBlock.gif        
/// 1。它仅仅可以应用到方法呀属性的scope下,用于返回值呀方法参数是不非法的。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        class ThisWillNeverCompile
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Error! var cannot be used as field data!
InBlock.gif
            private var myInt = 10;
InBlock.gif            
//Error! var cannot be used as return values  or parameter types!
ExpandedSubBlockStart.gifContractedSubBlock.gif
            public var MyMethod(var x, var y) dot.gif{ }            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         As well, local variables declared with the var keyword must be assigned an initial value at the
InBlock.gif         * 同时用var声明的局部变量也应该具有初使值,并且不可以赋为null 值(以便于进行类型的推断)。
InBlock.gif            // Error! Must assign a value!
InBlock.gif            var myData;
InBlock.gif            // Error! Must assign value at time of declaration!
InBlock.gif            var myInt;
InBlock.gif            myInt = 0;
InBlock.gif            // Error! Can't assign null!
InBlock.gif            var myObj = null;
InBlock.gif            It is permissible, however, to assign an inferred object local variable (已经被断定的变量)to null after its initial
InBlock.gif            assignment:
InBlock.gif            // OK!
InBlock.gif            var myCar = new SportsCar();
InBlock.gif            myCar = null;
InBlock.gif            //进一步这也是可以的,把隐式变量赋值缎带其它变量(包括隐式或预定的类型)
InBlock.gif            // Also OK!
InBlock.gif            var myInt = 0;
InBlock.gif            var anotherInt = myInt;
InBlock.gif            string myString = "Wake up!";
InBlock.gif            var myData = myString;
InBlock.gif            最后,我想要指出的是,过分的使用var 类型的变量,可以简代你的代码量,但是使代码的可读性降底了。
InBlock.gif         * 在这个时候,我们需要一定的软件构建知道帮我们进行
InBlock.gif            权衡。
InBlock.gif         * 
InBlock.gif         * 然页,我们会在LINQ技术中利用查询表达式,来产生动态的结果集,这那些例子中,implicit typing 是很有用的,
InBlock.gif         * 我们不需要去确定我们需要的结果集的类型,通过编译器的推断,
InBlock.gif         * 我们可以省去很多的时间。
InBlock.gif         * 
InBlock.gif
InBlock.gif         * 进一步的理解,local variables 类型推断并非与已有的脚本语言等同的技术,
InBlock.gif         * 如( (such as VBScript or Perl) or the COM Variant data type这些变量在程序的
InBlock.gif         * 生命周基中可以赋不同的类型,然而type inference 却是保持着c#强类型的特性,类型的确定是在编译时,而非运行时,
InBlock.gif         * // Error! The compiler knows myVar is a string, not an integer!
InBlock.gif            var s = "This variable can only hold string data!";
InBlock.gif            s = "This is finedot.gif";
InBlock.gif            s = 44; // This is not!
ExpandedSubBlockEnd.gif         
*/

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/snowball/archive/2008/01/22/1048911.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值