2

I want to make a method int.TryParse(string, out int number), but the number should be a null if it can't Parse instead of 0.

I have seen some other solutions, but none of them seem to actually allow for an entry type to be defined.

Basically what I'm asking is:

How can I change the "int" in int.TryParse to something else, e.g. int?.TryParse

    public static bool TryParse(this int? i, string input, out int? output)
    {
        var IsNumber = int.TryParse(input, out int result);

        if (!IsNumber)
            output = null;
        else
            output = result;
        return IsNumber; 
    }

This is what I have so far, but now I have to make an object of int? in order to use it instead of just being able to directly use it on the int? type.

What I have:

        var _string = Console.ReadLine();
        int? nInt = null;
        var IsNumber = nInt.TryParse(_string, out int? result);

What I want:

        var _string = Console.ReadLine();
        var IsNumber = int?.TryParse(_string, out int? result);

Stuff I checked:

Convert string to nullable type (int, double, etc...)

TryParse Nullable types generically


Pseudo-Answer:

public class Int
{
    public static bool TryParse(string input, out int? output)
    {
        var IsNumber = int.TryParse(input, out int result);

        if (!IsNumber)
            output = null;
        else
            output = result;
        return IsNumber;
    }
}

which can be then used as

var IsNumber = Int.TryParse(_input, out int? result);

returning a boolean and an instance 'result' having a value of int or null.

  • 1
    @Rawling they all use an instance of string. something I'm explicitely asking NOT to do... – Dennis Vanhout Jan 04 '19 at 14:04
  • 2
    Please edit your question body with why you disagree with the duplicate, not the question title. – Patrick Hofman Jan 04 '19 at 14:10
  • You can use int.TryParse. Your already noticed. Yet I wonder, let's say it would be possible to use int?.TryParse, what string input would result in null? Do you want to be able to process calues like null, 1, 2, 3, 4, 5, etc.? – Clark Kent Jan 04 '19 at 14:13
  • @ClarkKent , From the post: `the number should be a null if it can't Parse`. This sounds (to me) like if the `string input` was `cake` , instead of the result being set to `0`, it should be set to `null`. – Jaskier Jan 04 '19 at 14:15
  • so int?.TryParse("") results in true and null? or int?.TryParse("null") results in true and null? or int?.TryParse("cake") will result in true and null? or all?? what will result in false and null? – Clark Kent Jan 04 '19 at 14:22
  • like Symon says, a traditional TryParse return a number by default (the parsed number if possible, or 0 if not possible), I want it to return null if it can't instead of 0, since if a user enters 0, now there is no way to directly know if the TryParse was succesful or not, except by first checking the boolean. – Dennis Vanhout Jan 04 '19 at 14:33
  • @ClarkKent int?.TryParse results in a boolean (true or false) and an instance of Nullable (being int or null). whereas a traditional int.TryParse results in a boolean (true or false) and an instance of int. – Dennis Vanhout Jan 04 '19 at 14:34
  • What i'm trying to say is, I think that nullable.tryParse will should pretty much always result in true, because there's no way of telling when it's supposed to be a valid string. And I think that's the reason it can't be done. – Clark Kent Jan 04 '19 at 14:41
  • @ClarkKent Cases it would be true: "7" "78" "-9". Cases it would be false: "j4", "j", "". that's the same as in TryParse, this is about the return values of the 'out' object. – Dennis Vanhout Jan 04 '19 at 14:55
  • I see your point. I'm afraid that it can't be done (yet) in C#, as far as i'm aware. – Clark Kent Jan 04 '19 at 14:58
  • @ClarkKent that's what they said in the comments for the Answer as well. Added something that works and comes close to what I'm looking for to the question. (might actually be what I'm looking for even...) – Dennis Vanhout Jan 04 '19 at 15:01
  • Okay. Good luck! – Clark Kent Jan 04 '19 at 15:09

1 Answers1

3

You can't overload the Nullable<T> struct where T in an int here. Therefore you have to resolve to another way to be able to call that method.

You seems to be trying to make an extension method on int?, which actually it should be on string, and then you have to use a string instance to call it:

public static bool TryParse(this string input, out int? output)
{
    var IsNumber = int.TryParse(input, out int result);

    if (!IsNumber)
        output = null;
    else
        output = result;
    return IsNumber; 
}

Call it:

yourStringInstance.TryParse(out int? result);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Actually it's `Nullable`, not `int`. – Imantas Jan 04 '19 at 13:56
  • @Imantas corrected – Patrick Hofman Jan 04 '19 at 13:57
  • But the thing is, as I show with the examples, I want to do it without creating an instance. – Dennis Vanhout Jan 04 '19 at 13:58
  • @DennisVanhout No, that is not a solution (and that was just a comment on a typo). I don't see where my code creates another instance. Could you explain what you deem wrong in it? – Patrick Hofman Jan 04 '19 at 13:58
  • @PatrickHofman "yourstringinstance" literally is an instance of a string. unless I'm misunderstanding it. – Dennis Vanhout Jan 04 '19 at 14:00
  • So what is `_string` in your example then? – Patrick Hofman Jan 04 '19 at 14:00
  • an instance that is given in the signature. but the solution you provide results in _string.TryParse whereas I'm asking for type.TryParse(string – Dennis Vanhout Jan 04 '19 at 14:05
  • 1
    As I said in the first two words in my answer: "You can't". – Patrick Hofman Jan 04 '19 at 14:07
  • 2
    @DennisVanhout There is a [proposal](https://github.com/dotnet/roslyn/issues/996) to create static extension methods in C#, but as of today there is no way to create your own static method on `int?`. – D Stanley Jan 04 '19 at 14:19
  • @DStanley in other words, fingers crossed for C# 8 or C# 9. thanks :) Still stupid how Microsoft CAN do stuff like that and we as users can't... – Dennis Vanhout Jan 04 '19 at 14:37
  • 1
    "They" can't do it either. The language just doesn't support it @DennisVanhout – Patrick Hofman Jan 04 '19 at 14:38
  • 1
    @DennisVanhout "They" _could_ do it by adding that functionality to the compiler (which is not free), but technically "you" could too since roslyn is extensible :) – D Stanley Jan 04 '19 at 14:41
  • @DStanley Yes, indeed. – Patrick Hofman Jan 04 '19 at 14:42
  • Went through the proposal and did find a more-or-less solution to this though. Basically just make a new class and give that the wanted method. Kinda the Struct solution they proposed in some of the other threads. I would add that as an answer, but considering this has been marked as Duplicate I can't anymore. – Dennis Vanhout Jan 04 '19 at 14:50
  • So what does that add over the static class with extension method option? @DennisVanhout – Patrick Hofman Jan 04 '19 at 14:50
  • the fact that you don't use it as an extensionmethod anymore and don't have to make an instance. (there might be an auto-instanciating, but at the very least you don't have to create it yourself anymore) – Dennis Vanhout Jan 04 '19 at 14:52
  • *there might be an auto-instanciating* I don't think you quite understand how extension methods work. They are just static method like any other static method and extension method calls are rewritten by the compiler to regular static method calls. There is no "instantation" going on. – Patrick Hofman Jan 04 '19 at 14:53
  • marked this answer as correct since Comments more or less answered it in combination with provided answer. – Dennis Vanhout Jan 04 '19 at 15:04