我如何使用C#6英寸;使用静态"特征? [英] How do I use the C#6 "Using static" feature?
问题描述
我在看看一对夫妇的在 C#6的新特性,具体而言,
的使用静态的
I'm having a look at a couple of the new features in C# 6, specifically, "using static".
的思路如下,按照一对夫妇教程,我才发现,结果
相反的:
The idea is as follows, according to a couple of tutorials I found,
Instead of:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello world!");
Console.WriteLine("Another message");
}
}
您可以省略重复控制台
语句,使用使用静态类的新的C#6特点:
You can omit the repeated Console
statement, using the new C# 6 feature of using static classes:
using System.Console;
class Program
{
static void Main()
{
WriteLine("Hello world!");
WriteLine("Another message");
}
}
然而,这似乎不是为我工作。我得到的使用
语句的错误,他说:
的A使用命名空间
'指令只能应用于命名空间;控制台
'是一个类型不是一个命名空间。考虑一个使用静态
,而不是指令的
"A '
using namespace
' directive can only be applied to namespaces; 'Console
' is a type not a namespace. Consider a 'using static
' directive instead"
我使用的Visual Studio 2015年,我有生成语言版本设置为C#6.0的
I'm using visual studio 2015, and I have the build language version set to "C# 6.0"
怎么办?是MSDN博客的例子不正确的?为什么不这项工作?
What gives? Is the msdn blog's example incorrect? Why doesn't this work?
推荐答案
它出现的语法已略有改变,因为这些博客文章写。由于错误信息显示,添加静态
来的包含语句:
It appears the syntax has slightly changed since those blog posts were written. As the error message suggests, add static
to your include statement:
using static System.Console;
// ^
class Program
{
static void Main()
{
WriteLine("Hello world!");
WriteLine("Another message");
}
}
然后,您的code编译。
Then, your code will compile.
这篇关于我如何使用C#6英寸;使用静态"特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!