Enum is not clear

  • Thread starter Alexander Vasilevsky
  • Start date
A

Alexander Vasilevsky

Something I did not understand the joke humor
What's the magic of 0 in enums?

class Program
{
static void Main(string[] args)
{
Foo a_obj = new Foo();
SubjectStatus a_s = a_obj.Status;
Console.WriteLine(a_s + "-" + a_obj.Status);
a_obj.Status = SubjectStatus.New;
Console.WriteLine(a_obj.Status);
a_obj.Status = 0;
Console.WriteLine(a_obj.Status);
// a_obj.Status = 3;
// Console.WriteLine(a_obj.Status);
}
}

public class Foo
{
private SubjectStatus m_Status;

public virtual SubjectStatus Status
{
get { return m_Status; }
set { m_Status = value; }
}
}
/// <summary>
/// Subject Status
/// </summary>
public enum SubjectStatus
{
New = 3,
Actual = 5,
Acrhive = 6
}
And why, if the comment to remove the program does not compile? do not see
the logic.

http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas
Gift
 
J

Jack Jackson

Something I did not understand the joke humor
What's the magic of 0 in enums?

class Program
{
static void Main(string[] args)
{
Foo a_obj = new Foo();
SubjectStatus a_s = a_obj.Status;
Console.WriteLine(a_s + "-" + a_obj.Status);
a_obj.Status = SubjectStatus.New;
Console.WriteLine(a_obj.Status);
a_obj.Status = 0;
Console.WriteLine(a_obj.Status);
// a_obj.Status = 3;
// Console.WriteLine(a_obj.Status);
}
}

public class Foo
{
private SubjectStatus m_Status;

public virtual SubjectStatus Status
{
get { return m_Status; }
set { m_Status = value; }
}
}
/// <summary>
/// Subject Status
/// </summary>
public enum SubjectStatus
{
New = 3,
Actual = 5,
Acrhive = 6
}
And why, if the comment to remove the program does not compile? do not see
the logic.

Here is a comment (quoted at
<http://cleveralias.blogs.com/thought_spearmints/2004/01/more_c_enum_wac.html>)
by Eric Gunnerson of the C# development team:

Enums in C# do dual purpose. They are used for the usual enum use,
and they're also used for bit fields. When I'm dealing with bit
fields, you often want to AND a value with the bit field and check if
it's true.

Our initial rules meant that you had to write:

if ((myVar & MyEnumName.ColorRed) != (MyEnumName) 0)

which we thought was difficult to read. One alernative was to
define a zero entry:

if ((myVar & MyEnumName.ColorRed) != MyEnumName.NoBitsSet)

which was also ugly.

We therefore decided to relax our rules a bit, and permit an
implicit conversion from the literal zero to any enum type, which
allows you to write:

if ((myVar & MyEnumName.ColorRed) != 0)
 
J

Jeff Johnson

Something I did not understand the joke humor
What's the magic of 0 in enums?

Any variable of an enum type can be set to 0 without explicitly casting 0 to
that type of enum. Any other value must be cast.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top