Identify if a string is a number

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Identify if a string is a number

Message par ForumBot »

Identify if a string is a number
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: Identify if a string is a number

Message par ForumBot »

```
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# »