为什么这不编译?
int? number = true ? 5 : null;
Run Code Online (Sandbox Code Playgroud)
无法确定条件表达式的类型,因为'int'和<null>之间没有隐式转换
解释为什么无法为int赋值null,例如
int? accom = (accomStr == "noval" ? null : Convert.ToInt32(accomStr));
Run Code Online (Sandbox Code Playgroud)
这段代码出了什么问题?
为什么这不起作用?看起来像有效的代码.
string cert = ddCovCert.SelectedValue;
int? x = (string.IsNullOrEmpty(cert)) ? null: int.Parse(cert);
Display(x);
Run Code Online (Sandbox Code Playgroud)
我该怎么编码呢?该方法采用Nullable.如果下拉列表中选择了一个字符串,我需要将其解析为一个int,否则我想将null传递给该方法.
EmployeeNumber =
string.IsNullOrEmpty(employeeNumberTextBox.Text)
? null
: Convert.ToInt32(employeeNumberTextBox.Text),
Run Code Online (Sandbox Code Playgroud)
我经常发现自己想要做这样的事情(EmployeeNumber
是Nullable<int>
因为它是在LINQ到SQL的DBML对象,其中列允许NULL值的属性).不幸的是,编译器认为"'null'和'int'之间没有隐式转换",即使这两种类型在对自己的可空int的赋值操作中都是有效的.
由于内联转换需要在.Text字符串上发生(如果它不为空),因此Null合并运算符不是我能看到的选项.
据我所知,唯一的方法是使用if语句和/或分两步进行分配.在这种特殊情况下,我发现非常令人沮丧,因为我想使用对象初始化器语法,这个赋值将在初始化块中...
谁知道更优雅的解决方案?
下面的C#代码:
int? i;
i = (true ? null : 0);
Run Code Online (Sandbox Code Playgroud)
给我错误:
无法确定条件表达式的类型,因为'<null>'和'int'之间没有隐式转换
这不应该有效吗?我在这里失踪了什么?
可能重复:
c#为什么不能将可为空的int赋值为null
我试图将我的阅读器[3]对象的日期时间转换为null,如果论坛没有lastPostDate,但它说我错过了转换.错误:
无法确定条件表达式的类型,因为
<null>
'System.DateTime' 之间没有隐式转换
public class Forums
{
public List<Forum> GetForums()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CMS"].ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("sproc_Forums_GetForums", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.Default);
List<Forum> forums = new List<Forum>();
while (reader.Read())
{
var title = reader[6].ToString();
var threadCount = (int)reader[5];
var lastPostTitle = reader[4].ToString();
// below is where im having a problem
Nullable<DateTime> lastPostDate = (reader[3] == DBNull.Value ? null : Convert.ToDateTime(reader[3]));
var lastPostBy = reader[2].ToString();
var forumGroup …
Run Code Online (Sandbox Code Playgroud) 以下代码无法编译:
//int a = ...
int? b = (int?) (a != 0 ? a : null);
Run Code Online (Sandbox Code Playgroud)
为了编译,需要将其更改为
int? b = (a != 0 ? a : (int?) null);
Run Code Online (Sandbox Code Playgroud)
由于这两个b = null
和b = a
是合法的,这是没有意义的我.
为什么我们不得不强制null
转换为一个int?
为什么我们不能简单地为整个表达式提供一个显式类型转换(我知道在其他情况下可能)?
可能重复:
条件运算符无法隐式转换?
为什么null需要显式类型转换?
我有一个搜索,并没有找到一个很好的解释为什么发生以下情况.
我有两个具有共同接口的类,我尝试使用三元运算符初始化此接口类型的实例,如下所示但是无法编译错误"无法确定条件表达式的类型,因为之间没有隐式转换'xxx.Class1'和'xxx.Class2':
public ConsoleLogger : ILogger { .... }
public SuppressLogger : ILogger { .... }
static void Main(string[] args)
{
.....
// The following creates the compile error
ILogger logger = suppressLogging ? new SuppressLogger() : new ConsoleLogger();
}
Run Code Online (Sandbox Code Playgroud)
如果我明确地将第一个conditioin强制转换为我的界面,这是有效的:
ILogger logger = suppressLogging ? ((ILogger)new SuppressLogger()) : new ConsoleLogger();
Run Code Online (Sandbox Code Playgroud)
显然我总能做到这一点:
ILogger logger;
if (suppressLogging)
{
logger = new SuppressLogger();
}
else
{
logger = new ConsoleLogger();
}
Run Code Online (Sandbox Code Playgroud)
替代方案很好,但我不能完全理解为什么第一个选项因隐式转换错误而失败,因为在我看来,这两个类都是ILogger类型,我不是真的想要进行转换(隐式或显式) ).我敢肯定这可能是一个静态语言编译问题,但我想了解发生了什么.
以下是错的
public double? Progress { get; set; }
Progress = null; // works
Progress = 1; // works
Progress = (1 == 2) ? 0.0 : null; // fails
Run Code Online (Sandbox Code Playgroud)
无法确定条件表达式的类型,因为"double"和"<null>"之间没有隐式转换
我有以下课程:
abstract class AClass { }
class Foo : AClass { }
class Bar : AClass { }
Run Code Online (Sandbox Code Playgroud)
当我试图使用它们时:
AClass myInstance;
myInstance = true ? new Foo() : new Bar();
Run Code Online (Sandbox Code Playgroud)
此代码将无法编译,因为"无法确定条件表达式的类型,因为'CSharpTest.Class1.Foo'和'CSharpTest.Class1.Bar'之间没有隐式转换"
但是以下样本编译好了:
if (true)
{
myInstance = new Foo();
}
else
{
myInstance = new Bar();
}
Run Code Online (Sandbox Code Playgroud)
这也没问题:
myInstance = true ? (AClass) new Foo() : new Bar();
Run Code Online (Sandbox Code Playgroud)
要么
myInstance = true ? new Foo() : (AClass) new Bar();
Run Code Online (Sandbox Code Playgroud)
为什么条件运算符和if子句的行为有如此大的差异?