我是 C# 初学者,正在尝试一段新代码。以下代码未正确打印值:
namespace systemTypes
{
class Program
{
static void Main(string[] args)
{
CommonData<string>name = new CommonData<string>();
name.Value = "abcd";
CommonData<float>version = new CommonData<float>();
version.Value = 2.0F;
Console.WriteLine(
"generic object storing string val : {0}",
name.Value);
Console.WriteLine(
"generic object storing float val : {0}",
version.Value);
}
}
public class CommonData<T>
{
private T _data;
public T Value
{
get
{
return this._data;
}
set
{
this._data = value;
}
}
}
}
它将字符串值打印为空白,将 int 值打印为零到控制台。我需要实现一个默认构造函数吗?我在这里想念什么?