我试图访问数据:
1 DrugName Paracetamolum Coffeinum NULL NULL
有田野..
id-ProductName -Substance1...
这是模型:
namespace Pharma;
public partial class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string Substance1 { get; set; }
public string Substance2 { get; set; }
public string Substance3 { get; set; }
public string Substance4 { get; set; }
// constructor
public Product()
{
// ProductName ??= "";
// Substance1 ??= "";
// Substance2 ??= "";
// Substance3 ??= "";
// Substance4 ??= "";
if (ProductName == null)
{
ProductName = "";
}
if (Substance1 == null)
{
Substance1 = "";
}
if (Substance2 == null)
{
Substance2 = "";
}
if (Substance3 == null)
{
Substance3 = "";
}
if (Substance4 == null)
{
Substance4 = "";
}
}
}
我有Data is Null
个错误.
然后我修改了字段:
public int ProductId { get; set; }
public string ProductName { get; set; }
public string? Substance1 { get; set; }
public string? Substance2 { get; set; }
public string? Substance3 { get; set; }
public string? Substance4 { get; set; }
通过添加"?"然后它就起作用了. 但如果不起作用,为什么这不起作用:
if (Substance4 == null)
{
Substance4 = "";
}
我试图理解.Net的工作方式,我有一个问题. 为什么添加"?"有效,但如果声明不起作用呢?