@不打印. [英] @ is not printed.

查看:92
本文介绍了@不打印.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码.

I am using following code.

namespace Null
{
    class Program
    {
        enum Fruits
        {
            @apple =-1,
            Apple = 1,
            Grapes = 2,
            Orange = 3,           
        };
        static void getfruit(Fruits f)
        {
            Console.WriteLine(f);
        }
        static void getfruit(Nullable<fruits> f = Fruits.@apple)
        {

            try { Console.WriteLine(f); }
            catch { Console.WriteLine("Null"); }         
        }
        static void Main(string[] args)
        {
           getfruit(Fruits.Apple);
           getfruit();
           Console.Read();
        }
    }
}



输出:
苹果
苹果

为什么它不产生@apples作为输出?



Output:
Apple
apple

Why is it not producing @apples as output?

推荐答案

请参见C#语言规范 [
前缀"@"允许使用关键字作为标识符,这在与其他编程语言进行交互时非常有用.字符@实际上不是标识符的一部分,因此标识符可能会在其他字符中看到语言是没有前缀的普通标识符.带有@前缀的标识符称为逐字标识符.允许将@前缀用于不是关键字的标识符,但出于风格考虑,强烈建议不要使用.

因此,由于"@"不是标识符的一部分,因此@ myEnumValue.ToString()不会呈现"@"是合理的.
See C# Language Specification[^] ... ''@'' is not part of the identifier (in section 2.4.2 Identifiers):

"The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style."

So, since the ''@'' is not part of the identifier, then it is reasonable that @myEnumValue.ToString() will not render the ''@''.


OK.您的代码现在可以编译.

我很惊讶地发现,VS不会抛出编译错误,而是直接丢弃@,以便您的enum就像被编译一样
OK. Your code compiles now.

I am surprised to find that rather than throw a compile error, VS simply discards the @ so that your enum is compiled as if it was
enum Fruits
{
   apple = -1,
   Apple = 1,
   Grapes = 2,
   ...............
   ...............
}



这就是为什么@不打印的原因.正如Dalek Dave在回答中所说的那样,VS似乎将其视为空白.

您可以测试这种情况是否发生,因为当您尝试在代码中使用枚举时,intellisense仅显示修改后的版本.

两个建议:
1.如果@是对"apple"和
的重要测试,



which is why the @ doesn''t print. As Dalek Dave said in his answer VS seems to be treating it as whitespace.

You can test that this is happening because when you try to use the enum in code, intellisense only shows the modified version.

Two suggestions:
1. If the @ is important test for ''apple'' and

if (f == Fruits.apple)
{
  Console.WriteLine(''@'' + f);
}



2.再次编辑您的问题(或提出一个新问题),以描述您要达到的目标.可能有人可以提供替代方法.



2. Edit your question again (or ask a new question) describe what it is that you are trying to achieve. Someone may be able to offer an alternative.


枚举的名称中可能没有空格.
@被认为是不适用的空格字符.
enum may not have white spaces in it''s name.
The @ is considered a non applicable whitespace character.


这篇关于@不打印.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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