Answered by:
Using Enums

Question
-
Hi,
I am doing my best to write my code to best practices.
Here I have a if/else clause
if (this.process == "Group Alpha") { // do stuff... } else if (this.process == "Group Bravo") { // do stuff... }
There is a set list of groups. So according to best practice, I should use an enum. However, "Group Alpha" has a space in its name so below does not work...
Any other solution?Thanks!
if (this.process == Groups.Alpha) { // do stuff... } else if (this.process == Groups.Bravo) { // do stuff... } enum Groups { Alpha = "Group Alpha", Bravo = "Group Bravo" }
Thursday, March 1, 2012 4:38 PM
Answers
-
You can have a method taking a parameter of type Group by adding a few more lines:
public class Group { string value; Group(string value) { this.value = value; } public readonly Group Alpha = new Group("Group Alpha"); public readonly Group Beta = new Group("Group Beta"); public override string ToString() { return value; } }
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:05 AM
Thursday, March 1, 2012 5:13 PM -
Hi obrienkev,
The type of enum items could only be one of the integer types: byte, sbyte, short, ushort, int, uint, long, ulong. Your enum Groups won't get compiled.
Here I suggest that you declare and utilize your Groups enum as below, the type of every enum item value is int by default:
enum Groups { Alpha, Bravo } class Temp { public Groups process; public void Foo() { if (this.process == Groups.Alpha) { // do stuff... } else if (this.process == Groups.Bravo) { // do stuff... } } }
Have a nice day,Leo Liu [MSFT]
MSDN Community Support | Feedback to us
- Proposed as answer by Rudedog2 Friday, March 2, 2012 11:11 PM
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:07 AM
Friday, March 2, 2012 8:23 AM -
Creating a class means one more object on heap. So, I suggest you to either declare them as constants as below,
const string GroupAlpha = "Group Alpha"; const string GroupBravo = "Group Bravo"
Then simply use the constants as below,
if (this.process == GroupsAlpha) { // do stuff... } else if (this.process == GroupsBravo) { // do stuff... }
On the other hand, if you still want to use enum only (which is actually better), then change the type of process to Groups (enum). Then define the enumeration as below,
enum Groups { Alpha, Bravo }
Then use the enum in your code as,
if (this.process == Groups.Alpha) { // do stuff... } else if (this.process == Groups.Bravo) { // do stuff... }
Please mark this post as answer if it solved your problem. Happy Programming!
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:07 AM
Friday, March 2, 2012 10:42 AM -
You're working way, way to hard. C# 2.0 implemented iterator blocks that trivializes making Enumerators/Enumerables. As I said in my previous post, the other option would be to make a List<Group> and return that lists Enumerator.
In the end, I just broke down and made the class. Enjoy.
Oh, I also made it a struct, rather than a class, because it seemed to make sense that way. You can change it back to a class if you prefer; I designed it so that it should compile and run either way.
public struct Group { #region Code that is to be configured public static readonly Group Alpha = new Group("Group Alpha"); public static readonly Group Beta = new Group("Group Beta"); public static readonly Group Invalid = new Group("N/A"); public static IEnumerable<Group> AllGroups { get { yield return Alpha; yield return Beta; yield return Invalid; //... //add a yield return for all instances here. } } #endregion private string value; /// <summary> /// default constructor /// </summary> private Group() { //you can make this default value whatever you want. null is another option I considered, but you //shouldn't have this me anything that doesn't exist as one of the options defined at the top of //the page. value = "N/A"; } /// <summary> /// primary constructor /// </summary> /// <param name="value">The string value that this is a wrapper for</param> private Group(string value) { this.value = value; } /// <summary> /// Compares the Group to another group, or to a string value. /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { if (obj is Group) { return this.value.Equals(((Group)obj).value); } string otherString = obj as string; if (otherString != null) { return this.value.Equals(otherString); } throw new ArgumentException("obj is neither a Group nor a String"); } public override int GetHashCode() { return value.GetHashCode(); } /// <summary> /// returns the internal string tha this is a wrappe for. /// </summary> /// <param name="group"></param> /// <returns></returns> public static implicit operator string(Group group) { return group.value; } /// <summary> /// Parses a string and returns an instance that corresponds to it. /// </summary> /// <param name="input"></param> /// <returns></returns> public static Group Parse(string input) { return AllGroups.Where(item => item.value == input).FirstOrDefault(); } /// <summary> /// Syntatic sugar for the Parse method. /// </summary> /// <param name="other"></param> /// <returns></returns> public static explicit operator Group(string other) { return Parse(other); } public override string ToString() { return value; } }
- Edited by servy42 Friday, March 2, 2012 6:17 PM
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:06 AM
Friday, March 2, 2012 6:14 PM -
There are many ways to compare strings and enums, but the advise to stick with enums is faster.
class Program { static string Alpha = "Group Alpha"; static string Bravo = "Group Bravo"; static void Main(string[] args) { CompareAsString(Alpha); Console.ReadLine(); } static void CompareAsString(string group) { if ( CompareStringToGroups(group) ) { Console.WriteLine("Group Alpha detected."); } } private static bool CompareStringToGroups(string group) { return String.Compare(group, Groups.GroupAlpha.ToString(), CultureInfo.CurrentCulture, CompareOptions.IgnoreSymbols) == 0; } private static bool CompareStringToGroups(string group, Groups groupType) { return String.Compare(group, groupType.ToString(), CultureInfo.CurrentCulture, CompareOptions.IgnoreSymbols) == 0; } } enum Groups { GroupAlpha, GroupBravo }
Hope this helps.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:06 AM
Friday, March 2, 2012 9:01 PM -
@Rudedog using an IgnoreSymbols comparision would involve a fairly significant change to the function of the code. First off, it may be important for a string that doesn't have the whitespace to fail, next the strings may need to have symbols that C# variables aren't allowed to have, they might start with a number (or just be number). Next you may want the variable name to be entirely different from the value. If the value is rather long, awkward, has symbols, etc. you may want a 1-2 word abbreviation to be the variable name while the string value it represents could be anything. It's also possible that the variable name may have more meaning. For example, consider an enumeration in which the variable names are column names in some list and the value of the enumeration is some alphanumeric internal identifier (say a GUID).
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:06 AM
Friday, March 2, 2012 9:39 PM -
Hello,
You can use TypeConverter together with Extension Methods like so.
namespace Test { using System; using System.ComponentModel; using System.Globalization; using System.Reflection; [TypeConverter(typeof(EnumToStringConverter))] public enum Groups { [Description("Group Alpha")] Alpha, [Description("Group Bravo")] Bravo, } public class EnumToStringConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(Enum); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return destinationType == typeof(string); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value.GetType().BaseType != typeof(Enum)) { throw new ArgumentException(string.Format("The underlying type of '{0}' does not match '{1}'.", value.GetType().BaseType, typeof(Enum)), "value"); } if (destinationType != typeof(string)) { throw new ArgumentException(string.Format("Cannot convert to type '{0}', only '{1}' is supported.", destinationType, typeof(string)), "destinationType"); } string name = value.ToString(); MemberInfo field = value.GetType().GetField(name); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; return attribute != null ? attribute.Description : name; } } public static class EnumExtensions { public static string ToDescription(this Enum @enum) { return TypeDescriptor.GetConverter(@enum.GetType()).ConvertToString(@enum); } } internal class Program { public static void Main(string[] args) { Console.WriteLine(Groups.Bravo.ToDescription()); Console.ReadKey(true); } } }
EDIT:
Changed the name of the converter class from EnumTypeConverter to EnumToStringConverter
Alternatively, you can implement it directly in the extension method without a TypeConverter.
namespace Test { using System; using System.ComponentModel; using System.Reflection; public enum Groups { [Description("Group Alpha")] Alpha, [Description("Group Bravo")] Bravo, } public static class EnumExtensions { public static string ToDescription(this Enum @enum) { string name = @enum.ToString(); MemberInfo field = @enum.GetType().GetField(name); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; return attribute != null ? attribute.Description : name; } } internal class Program { public static void Main(string[] args) { Console.WriteLine(Groups.Bravo.ToDescription()); Console.ReadKey(true); } } }
I thought to add that using the TypeConverter version is in my opinion better because it's open for extensions.
Eyal (http://shilony.net), Regards.
- Edited by Eyal Solnik Saturday, March 3, 2012 8:21 AM
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:07 AM
Friday, March 2, 2012 11:18 PM
All replies
-
I've used a class similar to the following for cases like this. Not sure how some feel it falls in the 'best practices' realm, but I like it.
public static class Groups { public const string Alpha = "Group Alpha"; public const string Beta = "Group Beta"; }
So given this code your if block would be identical. It obviously can't do things like parse a string to an instance, and it's not strongly types (you can't have a method with a parameter of "Groups"). If you're doing other stuff that's really more enum-y it may not work, but if it's pretty much just what you've shown here I don't think it's a terrible idea.
Thursday, March 1, 2012 4:48 PM -
You can have a method taking a parameter of type Group by adding a few more lines:
public class Group { string value; Group(string value) { this.value = value; } public readonly Group Alpha = new Group("Group Alpha"); public readonly Group Beta = new Group("Group Beta"); public override string ToString() { return value; } }
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:05 AM
Thursday, March 1, 2012 5:13 PM -
If you really want to have it act more or less like an Enum you could take it further. I'd make the constructor of yours private, I'd add an implicit conversion operator to string, and an explicit conversion operator from string (or a parse/tryparase method, or both). You may want to override Equals and GetHashCode. Implementing IEnumerable<T> would also be an option.
I didn't mention any of this before simply because it may or may not really be needed, and in the past when I've used classes like this I haven't written it. Once you've done it once it pretty much becomes boilerplate code and you can just swap out the class name and the readonly instances to make a new version of it.
- Edited by servy42 Thursday, March 1, 2012 5:56 PM
Thursday, March 1, 2012 5:56 PM -
Hi obrienkev,
The type of enum items could only be one of the integer types: byte, sbyte, short, ushort, int, uint, long, ulong. Your enum Groups won't get compiled.
Here I suggest that you declare and utilize your Groups enum as below, the type of every enum item value is int by default:
enum Groups { Alpha, Bravo } class Temp { public Groups process; public void Foo() { if (this.process == Groups.Alpha) { // do stuff... } else if (this.process == Groups.Bravo) { // do stuff... } } }
Have a nice day,Leo Liu [MSFT]
MSDN Community Support | Feedback to us
- Proposed as answer by Rudedog2 Friday, March 2, 2012 11:11 PM
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:07 AM
Friday, March 2, 2012 8:23 AM -
If you really want to have it act more or less like an Enum you could take it further. I'd make the constructor of yours private, I'd add an implicit conversion operator to string, and an explicit conversion operator from string (or a parse/tryparase method, or both). You may want to override Equals and GetHashCode. Implementing IEnumerable<T> would also be an option.
I didn't mention any of this before simply because it may or may not really be needed, and in the past when I've used classes like this I haven't written it. Once you've done it once it pretty much becomes boilerplate code and you can just swap out the class name and the readonly instances to make a new version of it.
Hi servy42. This sounds interesting. Any code example of this? Thanks!Friday, March 2, 2012 10:25 AM -
Creating a class means one more object on heap. So, I suggest you to either declare them as constants as below,
const string GroupAlpha = "Group Alpha"; const string GroupBravo = "Group Bravo"
Then simply use the constants as below,
if (this.process == GroupsAlpha) { // do stuff... } else if (this.process == GroupsBravo) { // do stuff... }
On the other hand, if you still want to use enum only (which is actually better), then change the type of process to Groups (enum). Then define the enumeration as below,
enum Groups { Alpha, Bravo }
Then use the enum in your code as,
if (this.process == Groups.Alpha) { // do stuff... } else if (this.process == Groups.Bravo) { // do stuff... }
Please mark this post as answer if it solved your problem. Happy Programming!
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:07 AM
Friday, March 2, 2012 10:42 AM -
Hi obrienkev,
It would appear that what he's comparing the enum values to are strings (read in from somewhere probably not in code). If you convert each string to the corresponding int you're basically going to have a block of code just like what the OP has; if string == "literal 1" value = enum.Value1;
The type of enum items could only be one of the integer types: byte, sbyte, short, ushort, int, uint, long, ulong. Your enum Groups won't get compiled.
Here I suggest that you declare and utilize your Groups enum as below, the type of every enum item value is int by default:
Friday, March 2, 2012 2:47 PM -
"Hi servy42. This sounds interesting. Any code example of this? Thanks!"
They're each separate issues that are not particularly hard. I'm fairly confident that if you do a google search on any given item (mostly just to see the syntax of how to define it) that you should be able to work it out. For Equals and getHashCode you'll most likely just be calling the same methods on the strings that you're wrapping, for the conversion to string you just pass out the string you're wrapping, for the parse you look through all of the strings you have and return the wrapper that corresponds to their string (this one would likely be the most code). For a static IEnumerable if you just put each instance into a data structure of some sort you can return the enumerator of that structure. It'll be mostly just the tediousness of typing all of that out, which is why I say I haven't been compelled to do it before now. If you have trouble with any particular point, or get it all working, I'd be glad to take a look at it for you. I'll make any tweaks you might need to help it work better. (And very likely steal it for my own purposes while I'm at it.)
Friday, March 2, 2012 2:51 PM -
OK> this is what I have now. But not sure how I use Enumerator<T>. Following this - http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.getenumerator.aspx
public Groups() { } public const string GroupAlpha = "Group Alpha"; public const string GroupBravo = "Group Bravo"; } public class GroupsEnum : IEnumerator { public Groups[] _groups; // Enumerators are positioned before the first element // until the first MoveNext() call. int position = -1; public GroupsEnum(Groups[] list) { _groups = list; } object IEnumerator.Current { get { return Current; } } public Groups Current { get { try { return _groups[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } }
Friday, March 2, 2012 5:40 PM -
You're working way, way to hard. C# 2.0 implemented iterator blocks that trivializes making Enumerators/Enumerables. As I said in my previous post, the other option would be to make a List<Group> and return that lists Enumerator.
In the end, I just broke down and made the class. Enjoy.
Oh, I also made it a struct, rather than a class, because it seemed to make sense that way. You can change it back to a class if you prefer; I designed it so that it should compile and run either way.
public struct Group { #region Code that is to be configured public static readonly Group Alpha = new Group("Group Alpha"); public static readonly Group Beta = new Group("Group Beta"); public static readonly Group Invalid = new Group("N/A"); public static IEnumerable<Group> AllGroups { get { yield return Alpha; yield return Beta; yield return Invalid; //... //add a yield return for all instances here. } } #endregion private string value; /// <summary> /// default constructor /// </summary> private Group() { //you can make this default value whatever you want. null is another option I considered, but you //shouldn't have this me anything that doesn't exist as one of the options defined at the top of //the page. value = "N/A"; } /// <summary> /// primary constructor /// </summary> /// <param name="value">The string value that this is a wrapper for</param> private Group(string value) { this.value = value; } /// <summary> /// Compares the Group to another group, or to a string value. /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { if (obj is Group) { return this.value.Equals(((Group)obj).value); } string otherString = obj as string; if (otherString != null) { return this.value.Equals(otherString); } throw new ArgumentException("obj is neither a Group nor a String"); } public override int GetHashCode() { return value.GetHashCode(); } /// <summary> /// returns the internal string tha this is a wrappe for. /// </summary> /// <param name="group"></param> /// <returns></returns> public static implicit operator string(Group group) { return group.value; } /// <summary> /// Parses a string and returns an instance that corresponds to it. /// </summary> /// <param name="input"></param> /// <returns></returns> public static Group Parse(string input) { return AllGroups.Where(item => item.value == input).FirstOrDefault(); } /// <summary> /// Syntatic sugar for the Parse method. /// </summary> /// <param name="other"></param> /// <returns></returns> public static explicit operator Group(string other) { return Parse(other); } public override string ToString() { return value; } }
- Edited by servy42 Friday, March 2, 2012 6:17 PM
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:06 AM
Friday, March 2, 2012 6:14 PM -
There are many ways to compare strings and enums, but the advise to stick with enums is faster.
class Program { static string Alpha = "Group Alpha"; static string Bravo = "Group Bravo"; static void Main(string[] args) { CompareAsString(Alpha); Console.ReadLine(); } static void CompareAsString(string group) { if ( CompareStringToGroups(group) ) { Console.WriteLine("Group Alpha detected."); } } private static bool CompareStringToGroups(string group) { return String.Compare(group, Groups.GroupAlpha.ToString(), CultureInfo.CurrentCulture, CompareOptions.IgnoreSymbols) == 0; } private static bool CompareStringToGroups(string group, Groups groupType) { return String.Compare(group, groupType.ToString(), CultureInfo.CurrentCulture, CompareOptions.IgnoreSymbols) == 0; } } enum Groups { GroupAlpha, GroupBravo }
Hope this helps.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:06 AM
Friday, March 2, 2012 9:01 PM -
@Rudedog using an IgnoreSymbols comparision would involve a fairly significant change to the function of the code. First off, it may be important for a string that doesn't have the whitespace to fail, next the strings may need to have symbols that C# variables aren't allowed to have, they might start with a number (or just be number). Next you may want the variable name to be entirely different from the value. If the value is rather long, awkward, has symbols, etc. you may want a 1-2 word abbreviation to be the variable name while the string value it represents could be anything. It's also possible that the variable name may have more meaning. For example, consider an enumeration in which the variable names are column names in some list and the value of the enumeration is some alphanumeric internal identifier (say a GUID).
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:06 AM
Friday, March 2, 2012 9:39 PM -
@servy42 - Of course, you are correct. A switch statement might be useful to in this method, instead of sequences of if/elseif blocks.
What I posted works with the posted string samples. I also pointed out that enumerations are better than comparing the strings. The string comparisons would inevitably be slower, and they would also produce garbage that would eventually need to be collected.
To address your concerns, what is missing is something that validates the data before it even reaches a method that evaluates the data and acts upon it.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
Friday, March 2, 2012 10:57 PM -
Hello,
You can use TypeConverter together with Extension Methods like so.
namespace Test { using System; using System.ComponentModel; using System.Globalization; using System.Reflection; [TypeConverter(typeof(EnumToStringConverter))] public enum Groups { [Description("Group Alpha")] Alpha, [Description("Group Bravo")] Bravo, } public class EnumToStringConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(Enum); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return destinationType == typeof(string); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value.GetType().BaseType != typeof(Enum)) { throw new ArgumentException(string.Format("The underlying type of '{0}' does not match '{1}'.", value.GetType().BaseType, typeof(Enum)), "value"); } if (destinationType != typeof(string)) { throw new ArgumentException(string.Format("Cannot convert to type '{0}', only '{1}' is supported.", destinationType, typeof(string)), "destinationType"); } string name = value.ToString(); MemberInfo field = value.GetType().GetField(name); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; return attribute != null ? attribute.Description : name; } } public static class EnumExtensions { public static string ToDescription(this Enum @enum) { return TypeDescriptor.GetConverter(@enum.GetType()).ConvertToString(@enum); } } internal class Program { public static void Main(string[] args) { Console.WriteLine(Groups.Bravo.ToDescription()); Console.ReadKey(true); } } }
EDIT:
Changed the name of the converter class from EnumTypeConverter to EnumToStringConverter
Alternatively, you can implement it directly in the extension method without a TypeConverter.
namespace Test { using System; using System.ComponentModel; using System.Reflection; public enum Groups { [Description("Group Alpha")] Alpha, [Description("Group Bravo")] Bravo, } public static class EnumExtensions { public static string ToDescription(this Enum @enum) { string name = @enum.ToString(); MemberInfo field = @enum.GetType().GetField(name); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; return attribute != null ? attribute.Description : name; } } internal class Program { public static void Main(string[] args) { Console.WriteLine(Groups.Bravo.ToDescription()); Console.ReadKey(true); } } }
I thought to add that using the TypeConverter version is in my opinion better because it's open for extensions.
Eyal (http://shilony.net), Regards.
- Edited by Eyal Solnik Saturday, March 3, 2012 8:21 AM
- Marked as answer by Leo Liu - MSFT Tuesday, March 13, 2012 6:07 AM
Friday, March 2, 2012 11:18 PM -
Hi obrienkev,
So how is it going now with our friends' suggestions?
We are looking forward to hearing from you.
Have a nice day,Leo Liu [MSFT]
MSDN Community Support | Feedback to us
Monday, March 5, 2012 5:06 AM -
@aljodAv.BR, this might not be a good workaround for these people who chose to use enums for specific reasons such as.
a) Strongly-Typed Constants.
b) Bitwise-Operations.
c) Performance.
d) Thread-Safety.
Eyal (http://shilony.net), Regards.
Tuesday, March 13, 2012 4:45 PM -
Bad conclusions:
bad conclusions ? there was nothing to conclude here it's an observation, people may use enums for a reason and your solution might not work for them.
On the contrary, your code is not strongly-typed; try this:
You're right, you can always cast them to their underlying type, however, one of the reasons to use enums is to avoid this exactly and acces them by name rather than their underlying value, the reason I said Strongly Type Constants which represents the actual values.
Anyway, this requirement can be easily included.
On the contrary, your code is not ready for bitwise operations; you must change it. Just try to specify a Group variable that's a Group.Alpha and Group.Bravo (it's your hypothesis); you cannot do that in your code.I know that the code above isn't set to perform bitwise operations but again, I'm just saying that in general people might pick enums for that reason.
Clearly, this requirement is fulfilled by the BCL class I'm using. It's "factory-optimized", "factory-debugged", and "factory-implemented" with many features (propeties, methods, extension methods, and interfaces) that come for free!
On the contrary, your code is not ready for use; it must be debugged first.How anything here at all relates to performance ? anyhow, this fact cannot be turned around enums are just faster out of the box.
Most of the pieces of code I post in the forums aren't ready for production, I test that the code works I don't create tests for them simply because I'm not here to write fully blown production code but give an example/idea of how something might/may work.
It's the OP personal duty to bring it to production quality not mine.
Clearly, this is not a requirement stated by the OP, so, it's out of context.
Moreover, you don't know what that means.
Anyway, this requirement is fulfilled by the BCL class I'm using. It's "factory-thread-safe" for free; from MSDN:
" A Dictionary(Of TKey, TValue)can support multiple readers concurrently ... "
Your code cannot support multiple readers simultaneously. Your code is, clearly, not thread-safe!You don't have to write clearly in every sentence; just because you think that something is clear doesn't really make it for everyone.
I never said that these points are a requirement I only said that people might roll on enums because of these points, I'm not saying that your approach shouldn't or can't be used I'm just saying that for some cases it might not be the best choice, that's all. ;)
From the MSDN: Enum Class v 1 to Enum Class v 4
This type is safe for multithreaded operations.
P.s. If you can let go the old thread and move on to a place where both of us can share knowledge, contribute and learn from one another I'd glad that both for us and for the community.
Eyal (http://shilony.net), Regards.
- Edited by Eyal Solnik Thursday, March 15, 2012 8:32 AM
Thursday, March 15, 2012 7:31 AM -
@aljodAv.BR, wow! you surely have some sort of a syndrome, shame on you and your attitude.
You're welcome to learn nothing from me but don't tell me to get off your posts because I'll reply to whatever content I think I have something to add and in this case I do.
Most of the things you write like in the old post are subjectively just to disprove my points so I'll just ignore you and your silly attitude. ;)
Eyal (http://shilony.net), Regards.
- Edited by Eyal Solnik Friday, March 23, 2012 11:47 AM
Friday, March 23, 2012 11:25 AM -
You should really look at the mirror and figure who is the low minded here.
Not your post, sorry.
Eyal (http://shilony.net), Regards.
- Edited by Eyal Solnik Monday, April 2, 2012 3:26 AM
Monday, April 2, 2012 3:24 AM -
It's always possible to cast the underlying type to the enum, EVEN IF THE SPECIFIC VALUE DOES NOT MATCH ANY ENUM VALUE! Therefore, enum is not strongly typed (except in your personal C#).
Being able to cast any integer to an enum doesn't mean it's not strongly-typed. It only means that the enum can handle more values than the constant explictly stated in its definition. Try using any enum E where another type T is expected, and you should get an error, unless an implicit conversion exists from E to T.Monday, April 2, 2012 12:24 PM -
Being able to cast any integer to an enum doesn't mean it's not strongly-typed.
Surely it does.
I guess we don't have the same definition of 'strongly-typed'. Care to share yours?
It only means that the enum can handle more values than the constant explictly stated in its definition.
Therefore, it handles values of another type!
Of course they are values of the correct type. Because C# is strongly-typed, you cannot put in a variable a value not pertaining to its type. Before stating things like that, you should try it first. Cast any value you want to an enum type and check the type of the result.
Get Back to school, quickly!
Okay, if you need to, you can ask your teacher to test it for you. You can check the type of the result by calling its GetType method.
Tuesday, April 10, 2012 7:34 AM -
-
And you should know, what it means to cast ...
But why do I write an answer - you continue the same way as before so a ban will come quickly again ...
Konrad
Wednesday, April 11, 2012 10:03 PM -
>> I guess we don't have the same definition of 'strongly-typed'. Care to share yours?
It's easy, you can find it in popular text books.
Since you went to look in a textbook, you must have seen that type safety is a way to prevent type errors, not a chain to prevent programmers from working. In your own personal view of type safety, Liskov substitution principle doesn't work and you can't profit from polymorphism. I'm not sure if your own personal specification allows the user to type a number and get that number in a numeric variable.
But as you said the C# Language Specification is wrong
No, I didn't. I said that one sentence in it is wrong. The specification is not God's word. It is possible to find errors in it.
and you're no surprised to see the same error stated in popular C# books
Of course I'm not surprised. Misconceptions tend to spread. Ask your teacher.
, I'm pretty sure you're guided by whatever grows up in your mind... never on official documents and popular text books.
I'm not in school, I don't need a textbook. I surely know more about the C# specification than you. That's why I could point precisely a sentence in that specification that shows I'm not wrong. I think you never replied to that. I also pointed another sentence in the CLI documentation to support my assertions.
>> Of course they are values of the correct type. Because C# is strongly-typed, you cannot put in a variable a value not pertaining to its type. Before stating things like that, you should try it first
You should know that enum and integer (ulong, long, etc...) are 2 different types.
True. You cannot put an integer in an enum variable. That's because C# is type-safe.
Thursday, April 12, 2012 11:59 AM -
I'd get tired really fast for saying the same thing over and over again in each one of my posts but amazingly this guy keeps on banging the same drum.
Not once in his posts he shared is own real life experience the only thing I keep seeing is the C# specification as if it was a religion to him.
Eyal (http://shilony.net), Regards.
Thursday, April 12, 2012 1:20 PM -
I'm reasonably certain he's been banned again. You're all talking to yourselves.
It's best to not join/continue discussions with ban evaders. It encourages them to continue ban evading, often makes more works for mods if they need to "clean up" their content (and thus posts that quote it), and of course you get left talking to yourself when the ban evading account is banned.
Thursday, April 12, 2012 1:43 PM -