Identify if a string is a number

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Identify if a string is a number

Message par Clippy »

Identify if a string is a number
Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Re: Identify if a string is a number

Message par Clippy »

```
int n;
bool isNumeric = int.TryParse("123", out n);

```

**Update** As of C# 7:

```
var isNumeric = int.TryParse("123", out int n);

```

or if you don't need the number you can [discard](https://learn.microsoft.com/en-us/dotnet/csharp/discards) the out parameter

```
var isNumeric = int.TryParse("123", out _);

```

The *var* s can be replaced by their respective types!
Répondre

Revenir à « .NET & C# »