xsharp.eu • How to discard a value in X#?
Page 1 of 1

How to discard a value in X#?

Posted: Mon Nov 14, 2022 4:35 pm
by VR
Hello

when I want to convert a string to an int, I use Int.TryParse which returns true, when the string can be parsed and also has an out param with the string value.

Code: Select all

var str := "123"
if int.TryParse(str, out var strAsInt)
  // do something with the value strAsInt
endif
But how can I handle the situation, when I only want to check, if the string can be parsed as Int,. To use TryParse, I have to define a variable for the out parameter and if I don't use it in the code, I'll get a warning.

In C#, I would use _ to discard the value.

Code: Select all

  static bool StringIsInt(string value) 
  {
     return int.TryParse(value, out _);
  }
Is there a way to do the same in x# (2.13)

Kind regards

Volkmar

How to discard a value in X#?

Posted: Mon Nov 14, 2022 4:41 pm
by robert
Volkmar,
Just like C#:

Code: Select all

if Int32.TryParse(str, out var _)
we also allow

Code: Select all

if Int32.TryParse(str, out null)
You can use the discard variable (_) everywhere just like in C#


Robert

How to discard a value in X#?

Posted: Mon Nov 14, 2022 4:43 pm
by VR
Thanks for the super fast response :-)