2

I have an enum (let's say DemoEnum) and I want to parse a value to this enum. I am writing

DemoEnum value;
if(DemoEnum.TryParse("input", out value))
{
   this.Value = value;
}

Now the resharper suggests me, to use the base class qualifier.

I just want to know what's the benefit of using the base class qualifier?

1
  • ReSharper 5.1's warning text is: "Access to a static member of a type via a derived type". Commented Apr 30, 2012 at 13:47

4 Answers 4

6

Generally, it is a good idea to use the most generic solution possible.

DemoEnum.TryParse("input", out value)

Is the same call as (you're just making the static call from an inherited class rather than the base class):

Enum.TryParse<DemoEnum>("input", out value)

Using the base class qualifier (Enum) instead of your specific enum (DemoEnum) would insulate you from possible side effects of changing DemoEnum in the future. The reality is that you're really only going to run into issues if you change DemoEnum to a class without changing the name.

This is generally a larger issue when using classes (and ReSharper will give the same guidance in those situations).

Sign up to request clarification or add additional context in comments.

4 Comments

No translation is made, it is misleading to even suggest that.
@JeffMercado - I was using the word 'translated' to mean that the first statement compiles to the same IL as the second. Changed the wording.
I think it would be important to explain why the compiler decides it should be DemoEnum though. What you show in your example makes it look like it's the static type that decides it.
What's really annoying with resharper is that Enum.TryParse<DemoEnum> is flagged with the <DemoEnum> as being redundant! It also incorrectly 'corrects' the problem so a straight Enum.TryParse(). Looks like a bug to me...
1

The TryParse() method is a static method defined in the Enum class. Since your enum inherits everything from the Enum class, it also "inherits" static members. It's not true inheritance, the static members are just visible from the class.

The other answers are wrong in that some special translation is being done for you. All you are doing is accessing the static member from a derived class since all static members are accessible through your enum. The type of the identifier that you are accessing the static method from has no bearing at all on what the generic parameters are, only what the compiler is able to infer from them (or you explicitly provide).

To illustrate my point, consider these two enums:

enum First { A, B }
enum Second { A, B }

First firstVar;
Second secondVar;
// note we're using the `First` name
First.TryParse("A", out firstVar);  // valid, firstVar <= First.A
First.TryParse("B", out secondVar); // still valid, secondVar <= Second.B

// is equivalent to
Enum.TryParse<First>("A", out firstVar); // generic type is inferred from type of firstVar
Enum.TryParse<Second>("B", out secondVar); // generic type is inferred from type of secondVar

What ReSharper is telling you is that you should be accessing the static member from the class that defined actually the member. Why should you do this?

Consider what would happen if your derived type defined a static member with the same exact name. (it's not possible in this case with enums but applicable to classes in general) What would happen to your code then? Any code that accessed the static member through the derived class will take on the new value and you will probably get no new warning about it. This may or may not be desired behavior so ReSharper is preemptively warning you that you should use the actual class that defined it (the base).

Comments

0

Looks like Resharper is suggesting you use Enum.TryParse - http://msdn.microsoft.com/en-us/library/dd783499.aspx

I bet if you look at the IL... DemoEnum.TryParse is just doing a base.TryParse

Comments

-1

When you write DemoEnum.TryParse, the compiler transforms it to Enum.TryParse<DemoEnum>. There is no functional difference, and you don't really need to be insulated from possible side effects because you can't define methods in an enum type, so TryParse can't be redefined. My best guess is that it's a style preference.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.