Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

c# - Why do all TryParse overloads have an out parameter?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Updated Answer:

In more recent versions of C# you can declare the output parameter inline, which allows you to remove the line of code you don't want in your example:

string value = Console.ReadLine(); //Get a value from the user.

if (int.TryParse(value, out int num))
{
    Console.WriteLine("An integer");
}
else
{
    Console.WriteLine("Not an integer");
}

You can simply ignore the result in your code and no longer have that extra line. You still have the extra parameter, but so?

The underlying "why" is still the same and is unlikely to ever change. The method needed to return two things, a bool indicating success and an int indicating the resulting value if successful. (I can't think of another way to convey the result, can you?) Since a method can only return one thing, and a custom result type seems like overkill for this, the decision was made to return the bool and have the result be an out parameter. And once that decision was made, it has to remain for the duration of the language.

"They" certainly could add an overload that doesn't output in the int value. But why? Why expend the effort in designing, documenting, testing, and as we've seen perpetually supporting a method that serves no purpose but to save a few keystrokes for an extreme minority of developers? Again, very unlikely.

For such features you are certainly welcome to propose a change. It would be pretty cool to have a proposal accepted, I imagine. I doubt this one would be, but if you're passionate about it then by all means have at it.


Original Answer:

The short answer is, "Because that's how the method is defined." Perhaps by chance someone from the C# language team might find this question and provide reasoning into why decisions were made, but that doesn't really change much at this point. C# is a statically compiled language and the method signatures need to match, so that's just the way it is.

(Imagine if they changed this and broke .TryParse() on all existing codebases. That would be... bad.)

You might be able to work around this in your own code, though. Something as simple as an extension method could do the trick for you:

public static bool IsInt(this string s)
{
    int x = 0;
    return int.TryParse(s, out x);
}

Then in your code you'd just need to call that method from the string value:

string value = Console.ReadLine();
if (value.IsInt())
    Console.WriteLine("An integer");
else
    Console.WriteLine("Not an integer");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...