Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got this question in an assessment form:

Quote:
What method correctly extends the string class?


With 4 propositions:

public static string IsValidName(this string i, string value) {}
public void IsValidName(this string i, string value) {}
public string IsValidName(this string i, string value) {}
public static void IsValidName(this string i, string value) {}


What is the correct answer and why?

What I have tried:

I'm not asking you to answer for me. Assessment is already finished. I just don't get it on this question. Usage of this require static method so I can easily delete B and C. But what about A and D? Both are correct. With something named IsValid I also expect a return value.
Posted
Updated 2-Feb-21 14:28pm

1 solution

Quote:
But what about A and D? Both are correct.


No, only one is correct, and only technically so. I would expect any Is.... method to return a boolean value, true or false, thereby answering the question "is something valid".
 
Share this answer
 
Comments
Bastien Vandamme 2-Feb-21 22:10pm    
Agree. But this one is not in propositions. This is why string is better than void. So I selected A. But D is also correct. A compiler compile it.
Dave Kreskowiak 3-Feb-21 0:00am    
Just because the compiler compiles it doesn't mean it's correct. Naming the method Is... implies it's going to return something.

Also, strings are immutable, meaning they cannot be changed, so if an extension is going to do something to a string, it has to create a new string and return it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900