Actually a subject.

int result = 0; Int32.TryParse("10", out result); if(result != 0) DoSomething(); 

Those. I need to create a variable that I don't need. In my opinion, it's easier:

 if(Int32.TryParse("10") != null) DoSomething(); 

For this, the TryParse method only needs to return an int ?. However, the framework uses the first approach. Why?

    1 answer 1

    First, forget / do not know that the TryParse method (and not TryXXX by the way) returns a bool telling you whether the conversion succeeded. The method that returns the resulting conversion value is called Parse. For example:

     int res = Int32.Parse("123"); 

    Secondly, your example

     if(Int32.TryParse("10") != null) 

    wrong How do you think it should return an int (value-type) in one case and a null in the other (clearly indicating a reference-type)?

    However, you can write an extension matod to help solve the inconvenient syntax problem:

     namespace MyExtensions { public static class ParseExtender { public static int? ParseInt(this string source) { int res; return Int32.TryParse(source, out res) ? (int?)res : null; } } } 

    And if you really want versatility at all, you can do it like this:

     namespace MyExtensions { public static T? RightParse<T>(this string input) where T : struct { var converter = TypeDescriptor.GetConverter(typeof(T)); try { return (T?)converter.ConvertFromString(null, CultureInfo.InvariantCulture, input); } catch (Exception) { return null; } } } 

    Use this:

     Console.WriteLine("256".RightParse<int>() ?? -1); Console.WriteLine("256.2".RightParse<double>() ?? -1); Console.WriteLine("true".RightParse<bool>() ?? false); 

    Just remember to add using MyExtensions to use it all.

    • I suggest that TryParse return Nullable <int> I just don’t like Parse. He throws an extra, which is quite expensive. - Vasya_P
    • one
      firstly, Nullable <T> and T are two different types, and secondly, TryParse appeared much earlier than Nullable <T>. Moreover,> he throws an exception, which is quite expensive, what are you doing, that throwing an exception is expensive for you? (sure nothing). This most likely means that you are doing unnecessary optimization. The same goes for saving 4 bytes for TryParse. If the syntax is so inconvenient, you can write your own method and add it to the int with the help of extensions - DreamChild
    • In this particular case with int Nullable <int>, but in general yes Nullable <T>. Tryxxx and int? appeared with 2 framework. My question is not about the speed of work, but rather about the readability of the code. - Vasya_P
    • one
      Your readability difficulties are not very clear, more precisely, what will your version be more readable? In the current implementation, you need to: int res; int anotherVal = Int32.TryParse (myString, out res)? res: -1; In yours, as far as I understand it, like this: int anotherVal = Int32.TryParse ("10"). HasValue? Int32.TryParse ("10"). Value: -1; Readability is perhaps even worse, with Int32.TryParse you have to perform it twice (or, again, create an "unnecessary" variable. And then, I repeat, if readability is so important to you, then why not write your own method to solve this problem? - DreamChild
    • OK. Here is my tryparse [code] method public static int? MyTryParse (string value) {try {return Int32.Parse (value);} catch (ArgumentNullException) {return null; } catch (FormatException) {return null; }} [/ code] Then you can use it [code] ViewBag.Value = HomeController.MyTryParse ("10") ?? -one; [/ code] You can check if for null, without any unnecessary variables for the sake of variables. But I have to use Parse, catch exceptions, swallow them, developed the framework could do everything without extra expenses. - Vasya_P