Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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();
        }
    }
}


Output:
Apple
apple

Why is it not producing @apples as output?
Posted
Updated 30-Aug-10 10:38am
v3
Comments
Toli Cuturicu 30-Aug-10 16:16pm    
Reason for my vote of 5
Is this a test?
Henry Minute 30-Aug-10 17:17pm    
That (@null) is what I would expect. See Chris Trelawney-Ross's answer for the explanation.

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 '@'.
 
Share this answer
 
Comments
Toli Cuturicu 4-Sep-10 5:08am    
Reason for my vote of 5
Outstanding
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,
   ...............
   ...............
}


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. 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.
 
Share this answer
 
v2
Comments
GD6882 30-Aug-10 17:13pm    
Nothing just stumbled accross this case.
Try this null is an reserved Keyword. I tried it as @null=-1, and it allowed me to declare it.
Funny :)
Toli Cuturicu 4-Sep-10 5:10am    
Reason for my vote of 3
not quite so
enum may not have white spaces in it's name.
The @ is considered a non applicable whitespace character.
 
Share this answer
 
Comments
GD6882 30-Aug-10 17:11pm    
Thanks,
But @null=-1 is accepted. Why so since null as identifier is not allowed as it is resevered keyword.
Toli Cuturicu 4-Sep-10 5:08am    
Reason for my vote of 2
false
The code that you have posted does not compile in either VS2008 nor VS2010.

What version are you using?

Did you cut and paste the code or just type it in? If you typed it, I would recommend that you edit your question and copy/paste your code in.
 
Share this answer
 
Comments
GD6882 30-Aug-10 16:35pm    
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(); } }

Try this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900