得到的字符串枚举值 [英] Get enum value from string

查看:176
本文介绍了得到的字符串枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个在线评估,我想了解的结果,在这里我已经做了一个问题:

I did an online assessment and I'm trying to understand the results, and here one of the questions I did already:

Change到方法声明

所以,问题是这样的:

和报告说:

我不明白为什么我的答案是为部分正确的标记,我失去的东西吗?另外的问题说,这是一个字符串处理的问题,但对我来说没有任何与该主题。

I can't see why my answer is marked as partially right, am I missing something? Also the question says this is a string manipulation question, although to me it has nothing to do with that subject.

马可波罗

更新

答案C不正确的,我被我用var关键字,而不是实际的枚举的事实误认

Answer C is not right, I was mistaken by the fact that I used the var keyword and not the actual enum.

推荐答案

您已经选中的两个的选项。你已经在显示的鼠标指针一个是好的,但另一种是没有。

You've ticked two options. The one you've shown the mouse pointer at is fine, but the other one isn't.

尝试它,所以你可以看到为什么。请注意,.NET 4中,有 Enum.TryParse< TEnum> 这是通用的,在几个方面从而减少恼人。不幸的是,的TryParse 不受限的强制 TEnum 是一个枚举类型......它不可能是如果它在C#中声明,但可能是在IL。看到我的约束旋律项目一个替代方法在这里,如果你有兴趣。 (如果你只是想知道为什么你只得到了部分信用,我完全理解......)

Try it so you can see why. Note that as of .NET 4, there's Enum.TryParse<TEnum> which is generic, and thus less annoying in a few ways. Unfortunately, TryParse isn't constrained to force TEnum to be an enum type... which it couldn't be if it were declared in C#, but could be in IL. See my Unconstrained Melody project for an alternative approach here, if you're interested. (If you just want to know why you only got partial credit, I completely understand...)

(我同意它不是真正的字符串操作本身。 )

(And I agree it's not really "string manipulation" as such.)

这篇关于得到的字符串枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆