3

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

Change to method declaration

So the question is this:

enter image description here

enter image description here

and the report says:

enter image description here

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.

Marco

UPDATE

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

9
  • Yes, you have the correct answer. Full credit should be given for correct answers. This is quite stupid in my opinion. Commented Apr 27, 2013 at 15:19
  • @rmayer06 That´s what I though, because I could not see any mistake there. I could leave like this, and not worry about the results, but I wonder how many good developers lose good opportunities because of these mistakes. Commented Apr 27, 2013 at 15:20
  • Please mind that all are var. Commented Apr 27, 2013 at 15:21
  • Yes, answer C is not correct. Enum.Parse returns an object. This is no problem when using vars but not when you use a strongly typed enum like ConnectionState. Commented Apr 27, 2013 at 15:22
  • @NikhilAgrawal Oh I see, my mistake then, the 3rd option returns an object. Commented Apr 27, 2013 at 15:23

2 Answers 2

6

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

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

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

3 Comments

Thanks for your answer Jon, I really though the parse would return the enum value, not an object. It would be an easy one to spot if you were using the IDE, and I'm sure I've came across this issue before, anyway it is good to know :)
@Marco: Well at execution time it will return the enum value. But consider what the declaration of Parse has to look like, given that it's a non-generic method.
Yes, I missed that, too bad.
2

Question asks which one will return an enum. In that case fourth option is right. Third option will return an object(albeit your enum will be encapsulated in an object).

The trick here is var. All are assigned to var. So even if it returns an object or an enum all are accepted in var.

Here is proof.

enter image description here

3 Comments

Look at the Q in the quiz. It is asking which one returns an enum, it doesn't say which one compiles.
@HosseinNarimaniRad, Nikhil is right, if I use the actual enum rather that then a variant it won´t compile.
@Marco yes that's right, but before editing he said both options are correct.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.