How to discard a value in X#?

This forum is meant for questions and discussions about the X# language and tools
Post Reply
VR
Posts: 101
Joined: Sun Aug 23, 2020 3:07 pm
Location: Italy

How to discard a value in X#?

Post 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
User avatar
robert
Posts: 4473
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

How to discard a value in X#?

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
VR
Posts: 101
Joined: Sun Aug 23, 2020 3:07 pm
Location: Italy

How to discard a value in X#?

Post by VR »

Thanks for the super fast response :-)
Post Reply