Try again

200512270453934121.gif

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

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

namespace Net30NewFeature1
{
    
public class ImplicitlyTypedLocalVariableTest
    
{
        
public static void Test()
        
{
            Test4();           
        }


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

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


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

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

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

        public static void Test3()
        
{
            var evenNumbers 
= new int[] 2468 };
            var myMinivans 
= new List<string>();
            var myCar 
= new System.IO.FileInfo("");
        }

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

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


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

        }


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

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

        
/*
         As well, local variables declared with the var keyword must be assigned an initial value at the
         * 同时用var声明的局部变量也应该具有初使值,并且不可以赋为null 值(以便于进行类型的推断)。
            // Error! Must assign a value!
            var myData;
            // Error! Must assign value at time of declaration!
            var myInt;
            myInt = 0;
            // Error! Can't assign null!
            var myObj = null;
            It is permissible, however, to assign an inferred object local variable (已经被断定的变量)to null after its initial
            assignment:
            // OK!
            var myCar = new SportsCar();
            myCar = null;
            //进一步这也是可以的,把隐式变量赋值缎带其它变量(包括隐式或预定的类型)
            // Also OK!
            var myInt = 0;
            var anotherInt = myInt;
            string myString = "Wake up!";
            var myData = myString;
            最后,我想要指出的是,过分的使用var 类型的变量,可以简代你的代码量,但是使代码的可读性降底了。
         * 在这个时候,我们需要一定的软件构建知道帮我们进行
            权衡。
         * 
         * 然页,我们会在LINQ技术中利用查询表达式,来产生动态的结果集,这那些例子中,implicit typing 是很有用的,
         * 我们不需要去确定我们需要的结果集的类型,通过编译器的推断,
         * 我们可以省去很多的时间。
         * 

         * 进一步的理解,local variables 类型推断并非与已有的脚本语言等同的技术,
         * 如( (such as VBScript or Perl) or the COM Variant data type这些变量在程序的
         * 生命周基中可以赋不同的类型,然而type inference 却是保持着c#强类型的特性,类型的确定是在编译时,而非运行时,
         * // Error! The compiler knows myVar is a string, not an integer!
            var s = "This variable can only hold string data!";
            s = "This is fine";
            s = 44; // This is not!
         
*/

    }

}


posted on 2008-01-22 16:38  共同学习,共同进步  阅读(601)  评论(0编辑  收藏  举报