惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Troy Hunt's Blog
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
T
Tenable Blog
L
LINUX DO - 热门话题
V
Visual Studio Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
Know Your Adversary
Know Your Adversary
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
Netflix TechBlog - Medium
SecWiki News
SecWiki News
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Project Zero
Project Zero
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
Recorded Future
Recorded Future
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
S
Securelist
Spread Privacy
Spread Privacy
L
LangChain Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Last Watchdog
The Last Watchdog
Attack and Defense Labs
Attack and Defense Labs
博客园 - 司徒正美
Help Net Security
Help Net Security
L
Lohrmann on Cybersecurity
人人都是产品经理
人人都是产品经理
Forbes - Security
Forbes - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
博客园 - 【当耐特】
T
Tor Project blog

博客园 - 共同学习,共同进步

SQL Pretty Printer for SSMS 很不错的SQL格式化插件 GOOD LINK C# 代码片段 用简单代码破解Excel保护密码 罗克韦尔自动化(中国)责任有限公司 - 招聘软件开发实习生 思考1 NET Framework Library Source Code Now Available SQL IsEmptyOrNull [笔记] C# 3.0 新特性[3]-Understanding Object Initializers [笔记] C# 3.0 新特性[2]-Understanding Extension Methods 通用数据库存储过程代码--高效分页存储过程 listview and downloader TFS 2008 - Running two Build Agents on the Same Machine Team Foundation Blog Overview of Team Foundation Build Understanding CGI with C# IEnumerator 是所有非泛型枚举数的基接口 C# Programming Guide An Introduction to C# Generics
[笔记] C# 3.0 新特性[1]-implicitly typed local variables
共同学习,共同进步 · 2008-01-22 · via 博客园 - 共同学习,共同进步

面对着观念的快速更新,一片茫然,幸得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!
         
*/

    }

}