简体   繁体   中英

Enum TryParse with base class qualifier

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?

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).

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).

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM