C#中如何将int?强制转换为int类型??

潜浮生 2017-08-17 05:31:04
int?如何强制转换为int类型?求解
...全文
8924 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
ilikeff8 2017-08-22
  • 打赏
  • 举报
回复 1
强转是可以的,这么写是不会报错的,但如果a是null的话,运行时会提示无法转换

                    int? a=0;
                    int b = (int)a;
threenewbee 2017-08-22
  • 打赏
  • 举报
回复
int? a = a ?? 默认值; int b = a.Value;
正怒月神 2017-08-22
  • 打赏
  • 举报
回复
引用 12 楼 qq_28527151 的回复:
这个就是在使用SqlSugar ORM3框架的时候,利用拉姆达表达式时,where无法使用这种判断where(qq=>qq.id==userID??0); 不知道有没有啥好的方法??求解
int uid=userID??0; where(qq=>qq.id==uid); 这个问题只是在lambda没有取到IEnumerable成为本地数据前,不能使用c#语法。
gaoyang348 2017-08-21
  • 打赏
  • 举报
回复
int.Parse()
fengzhiqiang1981 2017-08-18
  • 打赏
  • 举报
回复
引用 6 楼 a755362405 的回复:
看代码示例
int? test = null;//定义一个int?赋值空
            int a = test ?? 0;//test??0  当test不为空时,直接返回值,为空时返回??后的值
这个应该满足你的需要, 为空取0, 不为空直接取值:   就是 ??  符号的作用
fengzhiqiang1981 2017-08-18
  • 打赏
  • 举报
回复
引用 5 楼 yuankaiwsl 的回复:
如果int?为空就不能转成int,如果这时能转成int,那int?类型就没必要存在了
这个应该满足你的需要, 为空取0, 不为空直接取值: 就是 ?? 符号的作用
snlixing 2017-08-18
  • 打赏
  • 举报
回复
int? n = null;

//int m1 = n;      // Will not compile.
int m2 = (int)n;   // Compiles, but will create an exception if x is null.
int m3 = n.Value;  // Compiles, but will create an exception if x is null.
键盘敲出字 2017-08-18
  • 打赏
  • 举报
回复
看代码示例
int? test = null;//定义一个int?赋值空
            int a = test ?? 0;//test??0  当test不为空时,直接返回值,为空时返回??后的值
巴士上的邂逅 2017-08-18
  • 打赏
  • 举报
回复
如果int?为空就不能转成int,如果这时能转成int,那int?类型就没必要存在了
by_封爱 版主 2017-08-18
  • 打赏
  • 举报
回复
同楼上..xx.value就是int了 何必强制转......
潜浮生 2017-08-18
  • 打赏
  • 举报
回复
引用 2 楼 sp1234 的回复:
先写出你对于这两种不同类型对象的转换规则来。
这个就是在使用SqlSugar ORM3框架的时候,利用拉姆达表达式时,where无法使用这种判断where(qq=>qq.id==userID??0); 不知道有没有啥好的方法??求解
xuzuning 2017-08-18
  • 打赏
  • 举报
回复
不可以!因为 int? 比 int 多了一个元素 null 你不可能将多出的 null 硬塞进 int 中,所以不能做强制类型转换
潜浮生 2017-08-18
  • 打赏
  • 举报
回复
不知道大家用过SqlSugar ORM框架没有,where(qq=>qq.id??0)这种写法就会存在这个问题。求解?
  • 打赏
  • 举报
回复
int? a; a.Value不就是int型?
  • 打赏
  • 举报
回复
先写出你对于这两种不同类型对象的转换规则来。
  • 打赏
  • 举报
回复
Convert.ToInt32()

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧