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

史考特

为什么

            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#中的三元运算符不信任您将表示nullint?您必须明确告诉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_data_type_id)是一个int?铸造它...

(int?)int.Parse(app_data_type_id)

三元收益率操作数中的任何一个都必须显式转换为int?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章