Better way to convert an int to a boolean(将 int 转换为布尔值的更好方法)
问题描述
输入的 int 值仅由 1 或 0 组成.我可以通过编写 if else 语句来解决问题.
The input int value only consist out of 1 or 0.
I can solve the problem by writing a if else statement.
有没有办法将 int 转换为 boolean?
Isn't there a way to cast the int into a boolean?
推荐答案
我假设 0 的意思是 false(很多编程语言都是这样).这意味着 true 是 not 0 (有些语言使用 -1 有些其他语言使用 1; 不会伤害到兼容).所以假设更好"意味着更少的打字,你可以写:
I assume 0 means false (which is the case in a lot of programming languages). That means true is not 0 (some languages use -1 some others use 1; doesn't hurt to be compatible to either). So assuming by "better" you mean less typing, you can just write:
bool boolValue = intValue != 0;
这篇关于将 int 转换为布尔值的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 int 转换为布尔值的更好方法
基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
