Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do all TryParse overloads have an out parameter? [closed]

Tags:

c#

tryparse

I have discovered that many times I don't need the out parameter of the TryParse method, but the problem that it is necessarily. Here I will show one example when it's not needed.

I want to check if a string is an integer, if it is an integer then print "An integer"; otherwise, print "Not an integer". So here is the code:

string value = Console.ReadLine(); //Get a value from the user. int num; //Why should I have it?? No need at all !  if (int.TryParse(value, out num)) {     Console.WriteLine("An integer"); } else {     Console.WriteLine("Not an integer"); } 

I am just wondering why TryParse always returns an out parameter? Why it doesn't have the overload without an out parameter?

like image 836
Misha Zaslavsky Avatar asked Oct 25 '13 14:10

Misha Zaslavsky


People also ask

What does out do in TryParse?

TryParse(myStr, out _). Obviously, there's still an out variable, but they explicitly made this to represent a parse where you don't care about the result. This clarifies code and is slightly shorter than ", out var num)".

Do you need declare an out variable before you use it?

Calling a method with an out argument In C# 6 and earlier, you must declare a variable in a separate statement before you pass it as an out argument. The following example declares a variable named number before it is passed to the Int32.

What is an out parameter in C#?

The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition​ as well as in the calling method.

Why should one use TryParse instead of parse?

Parse() method throws an exception if it cannot parse the value, whereas TryParse() method returns a bool indicating whether it succeeded. However, TryParse does not return the value, it returns a status code to indicate whether the parse succeeded and does not throw exception.


1 Answers

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"); 
like image 81
David Avatar answered Sep 24 '22 09:09

David