我如何使用“?” MVC 中的运算符?

发布于 2024-08-11 11:12:24 字数 223 浏览 3 评论 0原文

我有一个 MVC 应用程序,我想使用此 ? 选项。

请告诉我该怎么做。我已经写过:

int? isfeature;

但是,在将值插入数据库时​​,我该如何使用它?

它给出以下错误:

Cannot convert from 'int?' to 'int'.

I have an MVC application and I want to use this ? option.

Please tell me how I can do this. I have written:

int? isfeature;

but, while inserting the value in database, how do I use it?

It is giving following error:

Cannot convert from 'int?' to 'int'.

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

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

发布评论

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

评论(2

萝莉病 2024-08-18 11:12:24

整数?是 Nullable的简写,这意味着该值可以是整数或 null。将 Nullable转换为对于 int,使用

int? ni = 2;
int i = (int)ni;

or

int? ni = 2;
int i = ni.Value;

如果值为 null,则会抛出异常,因此您也可以使用

int? ni = null;
int i = ni ?? 0;

0 作为默认值。

int? is a shorthand for Nullable<int>, which means that the value can either be an integer or null. To convert a Nullable<int> to an int, use

int? ni = 2;
int i = (int)ni;

or

int? ni = 2;
int i = ni.Value;

This will throw an exception if the value is null, so you can also use

int? ni = null;
int i = ni ?? 0;

to use 0 as a default value.

七七 2024-08-18 11:12:24

你的意思是这样的吗?

$foo = $foo == 1 ? $foo : $bar;

因为我不太明白你的问题......

do you mean something like this?

$foo = $foo == 1 ? $foo : $bar;

Because I don't understand much of your question...

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