Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#可为null的整数-编译错误_C#_Nullable - Fatal编程技术网

C#可为null的整数-编译错误

C#可为null的整数-编译错误,c#,nullable,C#,Nullable,为什么 int? nullInt = null; base.Response.Data = (new BusinessLogic.RefDataManager(base.AppSettingsInfo)).SelectAppData(new DC.AppData() { AppDataKey = app_data_key != string.Empty ? app_data_key : null, AppDataTypeId = app_data_

为什么

            int? nullInt = null;
            base.Response.Data = (new BusinessLogic.RefDataManager(base.AppSettingsInfo)).SelectAppData(new DC.AppData() { AppDataKey = app_data_key != string.Empty ? app_data_key : null, AppDataTypeId = app_data_type_id != string.Empty ? int.Parse(app_data_type_id) : nullInt });
编译,但是这个

            base.Response.Data = (new BusinessLogic.RefDataManager(base.AppSettingsInfo)).SelectAppData(new DC.AppData() { AppDataKey = app_data_key != string.Empty ? app_data_key : null, AppDataTypeId = app_data_type_id != string.Empty ? int.Parse(app_data_type_id) : null});
不是吗?第二条语句的编译错误为“无法确定条件表达式的类型,因为“int”和null之间没有隐式转换”

DC.AppData是

public class AppData
{
    [DataMember(Name = "AppDataKey")]
    public string AppDataKey { get; set; }

    [DataMember(Name = "AppDataTypeId")]
    public int? AppDataTypeId { get; set; }


}
C#中的三元运算符不相信您将
null
表示为
int?
。你必须明确地告诉C编译器你的意思是
null
int?

base.Response.Data = (new BusinessLogic.RefDataManager(base.AppSettingsInfo)).SelectAppData(new DC.AppData() { AppDataKey = app_data_key != string.Empty ? app_data_key : null, AppDataTypeId = app_data_type_id != string.Empty ? int.Parse(app_data_type_id) : (int?)null});
…或者通过强制转换
int.Parse(app\u data\u type\u id)
int?

(int?)int.Parse(app_data_type_id)
任何一个三值生成操作数必须显式转换为
int?

C中的三值运算符不相信您将
null
表示为
int?
。你必须明确地告诉C编译器你的意思是
null
int?

base.Response.Data = (new BusinessLogic.RefDataManager(base.AppSettingsInfo)).SelectAppData(new DC.AppData() { AppDataKey = app_data_key != string.Empty ? app_data_key : null, AppDataTypeId = app_data_type_id != string.Empty ? int.Parse(app_data_type_id) : (int?)null});
…或者通过强制转换
int.Parse(app\u data\u type\u id)
int?

(int?)int.Parse(app_data_type_id)
必须将任一三元屈服操作数显式转换为
int?

问题在于:

app_data_type_id != string.Empty ? int.Parse(app_data_type_id) : null
Parse返回一个不可为空的int

你需要将它转换为int吗

(int?) int.Parse(app_data_type_id) : null
问题在于:

app_data_type_id != string.Empty ? int.Parse(app_data_type_id) : null
Parse返回一个不可为空的int

你需要将它转换为int吗

(int?) int.Parse(app_data_type_id) : null

为什么要把所有这些都塞进一行,如果你只使用多行,它会更可读,你不会有这样的问题。为什么要把所有这些塞进一行,如果你只使用多行,它会更可读,你不会有这样的问题。实际上,我们的两个答案都提供了一个有效的解决方案。似乎三元屈服操作数中的任何一个都需要显式地
int?
,但不是两者都需要。实际上,我们的两个答案都提供了一个有效的解决方案。似乎三元屈服操作数中的任何一个都需要显式地
int?
,但不能两者都是。