Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Is there a way to avoid mentioning int.TryParse at all, so that my calls look as follows:</p> </blockquote> <p>Not directly, as <code>TryParse</code> isn't part of a shared interface. If there were a shared interface to these value types, this would be possible via a constraint.</p> <hr> <p>Personally, I would not suggest using extension methods for this. I would rather write this as something more like:</p> <pre><code>public static class Parse { public delegate bool ParseDelegate&lt;T&gt;(string s, out T result); public static T? FromString&lt;T&gt;(string value, ParseDelegate&lt;T&gt; parse) where T : struct { T result; var parsed = parse(value, out result); return parsed ? result : (T?)null; } public static int? ToNullableInt32(string value) { return FromString&lt;int&gt;(value, int.TryParse); } public static double? ToNullableDouble(string value) { return FromString&lt;double&gt;(value, double.TryParse); } } </code></pre> <p>This adds a bit of overhead up front, but allows you to write these very cleanly, ie:</p> <pre><code> int? first = Parse.FromString&lt;int&gt;("1234", int.TryParse); int? second = Parse.ToNullableInt32("1234"); double? third = Parse.ToNullableDouble("1234"); </code></pre> <p>I see little value in putting an extension method, especially on something like <code>string</code> (which is used everywhere), as it "pollutes" the compilation of string itself. You'll see this everywhere you use strings - basically, any time you use this namespace, you'll end up having these parse methods in your intellisense, etc. In addition, this seems more like a "utility" than something that should appear as built-in functionality of string itself, which is why I personally prefer a separate class for it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload