C# int.TryParse = null 如果不是数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1729455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:20:02  来源:igfitidea点击:

int.TryParse = null if not numeric?

c#.nettryparse

提问by Lasse Edsvik

is there some way of return null if it can't parse a string to int?

如果无法将字符串解析为 int,是否有某种方法可以返回 null?

with:

和:

public .... , string? categoryID) 
{
int.TryParse(categoryID, out categoryID);

getting "cannot convert from 'out string' to 'out int'

得到“无法从'out string'转换为'out int'

what to do?

该怎么办?

EDIT:

编辑:

No longer relevant because of asp.net constraints is the way to solve problem

由于asp.net约束不再相关是解决问题的方法

/M

/M

采纳答案by Joey

First of all, why are you trying to parse a string to an int and stick the result back into a string?

首先,为什么要尝试将字符串解析为 int 并将结果粘贴回字符串?

The method signature is

方法签名是

bool int.TryParse(string, out int)

so you have to give a variable of type intas second argument. This also means that you won't get nullif parsing fails, instead the method will simply return false. But you can easily piece that together:

所以你必须给一个类型的变量int作为第二个参数。这也意味着null如果解析失败,您将不会得到,而是该方法将简单地返回false。但是您可以轻松地将它们拼凑在一起:

int? TryParse2(string s) {
    int i;
    if (!int.TryParse(s, out i)) {
        return null;
    } else {
        return i;
    }
}

回答by Dani

** this answer was down-voted a lot ** Although it is a possible solution - it is a bad one performance wise, and probably not a good programming choice.

** 这个答案被否决了很多 ** 虽然这是一个可能的解决方案 - 它在性能方面很糟糕,而且可能不是一个好的编程选择。

I will not delete it, as I guess many programmers might not be aware of this, so here is an example how not to do things:

我不会删除它,因为我想很多程序员可能没有意识到这一点,所以这里是一个如何不做事的例子:

use try and catch

使用尝试和捕获

try
{
res = Int32.Parse(strVAR)
}
catch(exception ex) 
{
 return null;
}

回答by Jason Punyon

Int is a value type which means there is no such thing as a null int. So no, TryParse will never alter the out parameter so that it is null.

Int 是一种值类型,这意味着没有 null int 之类的东西。所以不,TryParse 永远不会改变 out 参数,使其为空。

But the problem you're having is you're passing a string to the out parameter of TryParse when its expecting an integer.

但是您遇到的问题是,当它需要一个整数时,您将一个字符串传递给 TryParse 的 out 参数。

You need something like this...

你需要这样的东西......

Int categoryID = 0;
string strCategoryID = "somestringmaybeitsaninteger";

int.TryParse(strCategoryID, out categoryID);

回答by Konamiman

TryParsewill return false if the string can't be parsed. You can use this fact to return either the parsed value, or null. Anyway I guess that you are intending to return int?from your method, then it would be something like this:

TryParse如果无法解析字符串,则返回 false。您可以使用此事实来返回已解析的值或 null。无论如何,我猜你打算int?从你的方法中返回,那么它会是这样的:

public int? ParseInt(string categoryID) 
{
    int theIntValue;
    bool parseOk = int.TryParse(categoryID, out theIntValue);
    if(parseOk) {
        return theIntValue;
    } else {
        return null;
    }
}

回答by jason

Here's a proper use of Int32.TryParse:

这是一个正确的使用Int32.TryParse

int? value;
int dummy;
if(Int32.TryParse(categoryID, out dummy)) {
    value = dummy;
}
else {
    value = null;
}
return value;

回答by Stefan Steinegger

Do you want to do something like this?

你想做这样的事情吗?

public int? Parse(string categoryID) 
{
  int value;
  if (int.TryParse(categoryID, out value))
  {
    return value;
  }
  else
  {
    return null;
  }
}

回答by DigitalNomad

How about this?

这个怎么样?

public int? ParseToNull(string categoryId)
{
    int id;
    return int.TryParse(categoryId, out id) ? (int?)id : null;
}

回答by user5000501

Simplest and one-liner...

最简单的单线...

int N = int.TryParse(somestring, out N) ? N : 0;

It works 'cause it's evaluated left to right. Null not so easy.

它有效,因为它是从左到右计算的。空不是那么容易。