在C#中使用var关键字 [英] usage of var keyword in C#

查看:73
本文介绍了在C#中使用var关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此声明来自MSDN的'var'的备注部分:



如果名为var的类型在范围内,则var关键字将解析为类型名称,不会被视为隐式类型的局部变量声明的一部分。



我对这句话没有清楚的理解。任何人都可以详细说明吗?



https:/ /msdn.microsoft.com/en-us/library/bb384061.aspx [ ^ ]

This statement is from Remark section of 'var' from MSDN:

"If a type named var is in scope, then the var keyword will resolve to that type name and will not be treated as part of an implicitly typed local variable declaration."

I am not getting clear understanding on this remark. Could anyone elaborate it?

https://msdn.microsoft.com/en-us/library/bb384061.aspx[^]

推荐答案

var 通常是C# - 用于声明变量的关键字,编译器可以通过右侧的赋值推断出类型:

var normally is a C#-Keyword that is used to declare a variable of which the compiler can infer the type by the assignment on the right side:
var i = 1;            // i is of type Int32
var s = "abc";        // s is of type String
var d = DateTime.Now; // d is of type DateTime

var z; // not allowed because the compiler can't infer any type



但是你可以定义一个名为var的自有类型(类或结构):


But you are allowed to define an own type (a class or struct) with the name "var":

class var
{
   // ...
}

var x; // x is of type var



如果名为var的类型在范围 - 意味着上面的示例可以在您知道名为 var 的类型的声明的任何地方工作。如果要在不同的命名空间中声明类型 var 而不使用using-namespace-directive指定该命名空间,则编译器将不知道该类型并再次处理var的出现作为C#关键字。



这至少应该只是学术兴趣,实际定义一个名为 var 因为它会激怒所有看代码的人。


"If a type named var is in scope" - means that the example above would work wherever your declaration of a type named var is known at. If you would declare the type var in a different namespace and not specify that namespace with a using-namespace-directive, then the compiler wouldn't know about that type and again treat an occurence of "var" as the C#-keyword.

This is -or at least should be- of academic interest only, it would be very bad practice to actually define a usertype named var because it will irritate everyone looking at the code.


将以下内容粘贴到visual studio控制台项目中并编译它:



paste the following in a visual studio console project and compile it:

namespace ATypeNamedVar
{
    class JustCreatingAScope
    {
        class var
        {
            public string SomeProperty { get; set; }
        }

        public static void AMethodUsingTheVarClass()
        {
            //var thisWillNotCompile = 1;
            var thisWillCompile = new var();
            Console.WriteLine("The variable thisWillCompile is of type: " + thisWillCompile.GetType().ToString());
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            var regularUse = 1;
            Console.WriteLine("The variable thisWillCompile is of type: " + regularUse.GetType().ToString());

            JustCreatingAScope.AMethodUsingTheVarClass();
        }
    }
}





请注意visual studio中的语法高亮:

在第一次使用时,你会得到类型着色而不是关键字着色(与上面不同)



所以:回答你的问题:如果你有一个名为var的类型,如上所述,如果使用单词var,它将由编译器解释为该类型而不是关键字var。这就是声明



Note the syntax highlighting in visual studio:
In the first use you get the type colouring and not the keyword coloring (unlike in the above)

So: to answer your question: if you have a type named var, like in the above, if you use the word var it will be interpreted by the compiler is that type and not as the keyword var. That is why the statement

var thisWillNotCompile = 1



无法编译的原因,因为你无法为type var。


does not compile, because you cannot assign an integer to the type var.


这篇关于在C#中使用var关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆