C#/.NET Little Pitfalls: Implicit Zero To Enum Conversion(www.blackrabbitcoder.net)

submitted by BlackRabbitCoderBlackRabbitCoder(661) 4 months, 1 day ago

Many times, we create overloaded methods or constructors to allow them to accept different kinds of data. Further, there are times that we may accept object when any value will do. This works well (aside from boxing/unboxing concerns for value types), but if you have an overload that accepts object and one that takes an enum, and you pass a constant expression of 0, where does it go?

3 comments |category: |Views: 212

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by BlackRabbitCoderBlackRabbitCoder(661) 4 months, 1 day ago 0

new post

Reply

posted by dpetersondpeterson(4397) 4 months ago 0

Now, the reason lies in the implementation of Equals correct? Since the default first enumerator for the enum is zero? Does the same problem happen if you pass in the literal value of 1, corresponding to another item in your enum?

Reply

posted by BlackRabbitCoderBlackRabbitCoder(661) 3 months, 28 days ago 0

Actually, no. It is purely an implicit conversion in the language itself from a const zero expression to an enum. 1, etc, won't make the conversion implicitly. Zero to enum is a special case.

Reply

information Login or create an account to comment on this story