我正试图用静态属性作为主题,但我不知道哪里出了问题。这是代码。
public static string Id
{
get { return Id; }
set
{
if (Id.Length < Idlen)
{
var zero = new string('0', Idlen - Id.Length);
Id = zero + Id;
Id = value;
}
else
{
Id = Id.Substring(Id.Length - Idlen);
Id = value;
}
}
}
public static int Idlen { get; set; }
答案 0 :(得分:1)
因此,这是您正在尝试实现的示例。 (尽管ReSharper告诉我不要使用这些字段名称...)
private static string _Id;
public static string Id
{
get => _Id;
set // You want to use value here (new value), not Id (old value)
{
if (value == null)
{
// Consider what you want to do if user calls Id = null
_Id = new string('0', Idlen);
}
else if (value.Length < Idlen)
{
var zero = new string('0', Idlen - value.Length);
_Id = zero + value;
}
else
{
_Id = value.Substring(value.Length - Idlen);
}
}
}
public static int Idlen { get; set; }