我试图在一个项目上工作,我想要一个可为空的属性。创建一个可为空的对象。可以做到吗?
NullableClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class NullableClass
{
public Guid ID { get; set; }
public string Name { get; set; }
public NullableClass()
{ }
public NullableClass(string Name)
{
this.Name = Name;
}
}
}
MainClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class MainClass
{
public Guid ID { get; set; }
public string Name { get; set; }
puplic int? Number { get; set; }
public NullableClass? NullableClass { get; set; }
public MainClass()
{ }
public MainClass(string Name)
{
this.Name = Name;
}
}
}
的Visual Studio提供了以下错误:
The type 'NullableClass' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>'
我怎样才能让我的财产:NullableClass? NullableClass
当我谷歌他们不说,为什么这不能做,但他们也没有说如何做到这一点。
所以我的问题是以下。 我可以创建可为空的对象吗? 是吗? - >如何? 不是吗? - >为什么不呢?
引用类型已经_nullable_ –