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

发布于 2024-08-10 17:38:53 字数 267 浏览 9 评论 0原文

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

with:

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

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

该怎么办?

编辑:

方法

由于asp.net约束而不再相关是解决问题/M的

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

with:

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

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

what to do?

EDIT:

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

/M

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

清欢 2024-08-17 17:38:53

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

方法签名是

bool int.TryParse(string, out int)

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

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

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

The method signature is

bool int.TryParse(string, out int)

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

int? TryParse2(string s) {
    int i;
    if (!int.TryParse(s, out i)) {
        return null;
    } else {
        return i;
    }
}
烟火散人牵绊 2024-08-17 17:38:53

这是 Int32.TryParse 的正确用法

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

Here's a proper use of Int32.TryParse:

int? value;
int dummy;
if(Int32.TryParse(categoryID, out dummy)) {
    value = dummy;
}
else {
    value = null;
}
return value;
浅黛梨妆こ 2024-08-17 17:38:53

这个怎么样?

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

How about this?

public int? ParseToNull(string categoryId)
{
    int id;
    return int.TryParse(categoryId, out id) ? (int?)id : null;
}
微暖i 2024-08-17 17:38:53

最简单且一行行...

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

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

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.

娇俏 2024-08-17 17:38:53

如果字符串无法解析,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;
    }
}

TryParse will 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:

public int? ParseInt(string categoryID) 
{
    int theIntValue;
    bool parseOk = int.TryParse(categoryID, out theIntValue);
    if(parseOk) {
        return theIntValue;
    } else {
        return null;
    }
}
撩动你心 2024-08-17 17:38:53

你想做这样的事情吗?

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

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;
  }
}
再见回来 2024-08-17 17:38:53

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

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

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

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

int.TryParse(strCategoryID, out categoryID);

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.

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

You need something like this...

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

int.TryParse(strCategoryID, out categoryID);
夜司空 2024-08-17 17:38:53

**这个答案被否决了很多**
尽管这是一种可能的解决方案,但从性能角度来看,它是一个糟糕的解决方案,并且可能不是一个好的编程选择。

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

使用 try 和 catch

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

** 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文