相关疑难解决方法(0)

无法确定条件表达式的类型,因为'int'和<null>之间没有隐式转换

为什么这不编译?

int? number = true ? 5 : null;
Run Code Online (Sandbox Code Playgroud)

无法确定条件表达式的类型,因为'int'和<null>之间没有隐式转换

c# nullable

141
推荐指数
4
解决办法
6万
查看次数

c#为什么不能将nullable int赋值为null

解释为什么无法为int赋值null,例如

int? accom = (accomStr == "noval" ? null  : Convert.ToInt32(accomStr));
Run Code Online (Sandbox Code Playgroud)

这段代码出了什么问题?

c# nullable

125
推荐指数
3
解决办法
12万
查看次数

C#代码无法编译.null和int之间没有隐式转换

可能重复:
可空类型和三元运算符:为什么是`?10:null`禁止?

为什么这不起作用?看起来像有效的代码.

  string cert = ddCovCert.SelectedValue;
  int? x = (string.IsNullOrEmpty(cert)) ? null: int.Parse(cert);
  Display(x);
Run Code Online (Sandbox Code Playgroud)

我该怎么编码呢?该方法采用Nullable.如果下拉列表中选择了一个字符串,我需要将其解析为一个int,否则我想将null传递给该方法.

c# string null nullable

81
推荐指数
2
解决办法
4万
查看次数

使用Nullable <value>类型的条件运算符赋值?

EmployeeNumber =
string.IsNullOrEmpty(employeeNumberTextBox.Text)
    ? null
    : Convert.ToInt32(employeeNumberTextBox.Text),
Run Code Online (Sandbox Code Playgroud)

我经常发现自己想要做这样的事情(EmployeeNumberNullable<int>因为它是在LINQ到SQL的DBML对象,其中列允许NULL值的属性).不幸的是,编译器认为"'null'和'int'之间没有隐式转换",即使这两种类型在对自己的可空int的赋值操作中都是有效的.

由于内联转换需要在.Text字符串上发生(如果它不为空),因此Null合并运算符不是我能看到的选项.

据我所知,唯一的方法是使用if语句和/或分两步进行分配.在这种特殊情况下,我发现非常令人沮丧,因为我想使用对象初始化器语法,这个赋值将在初始化块中...

谁知道更优雅的解决方案?

c# nullable conditional-operator

58
推荐指数
3
解决办法
2万
查看次数

为什么我不能在三元if语句中将可空int设置为null?

下面的C#代码:

int? i;
i = (true ? null : 0);
Run Code Online (Sandbox Code Playgroud)

给我错误:

无法确定条件表达式的类型,因为'<null>'和'int'之间没有隐式转换

这不应该有效吗?我在这里失踪了什么?

c# asp.net

43
推荐指数
2
解决办法
2万
查看次数

可以为空的DateTime转换

可能重复:
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)

c# sql asp.net entity-framework

26
推荐指数
4
解决办法
9万
查看次数

为什么null需要显式类型转换?

以下代码无法编译:

//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 = nullb = a是合法的,这是没有意义的我.

为什么我们不得不强制null转换为一个int?为什么我们不能简单地为整个表达式提供一个显式类型转换(我知道在其他情况下可能)?

.net c# null casting

21
推荐指数
3
解决办法
2599
查看次数

三元条件下的隐式转换问题

可能重复:
条件运算符无法隐式转换?
为什么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类型,我不是真的想要进行转换(隐式或显式) ).我敢肯定这可能是一个静态语言编译问题,但我想了解发生了什么.

c# compiler-errors implicit-conversion

21
推荐指数
2
解决办法
3139
查看次数

将nullable double设置为null有什么问题?

可能重复:
为什么我不能在三元if语句中将可空int设置为null?
可空类型和三元运算符.为什么这不起作用?

以下是错的

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>"之间没有隐式转换

c#

15
推荐指数
1
解决办法
2万
查看次数

使用条件运算符时没有隐式转换

我有以下课程:

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子句的行为有如此大的差异?

c# oop

15
推荐指数
2
解决办法
4232
查看次数