错在哪里了?

来源:百度知道 编辑:UC知道 时间:2024/06/07 16:31:55
using System;
using System.Collections.Generic;
using System.Text;

namespace c5
{
class Program
{
static void Main(string[] args)
{
c5 n = new c5();
n._Id="123";
n.show();
}
}
class c5
{
public string _Id
{
get
{
return _Id;
}
set
{
// 验证输入长度小于 2
if (_Id.Length > 2)
_Id = value;
}
}
public void show()
{
Console.WriteLine(this._Id);
}
}
}

错误:
你自定义的类C5中有错误,有属性_Id。那么属性中赋值和读取不应该有_Id,应该定义一个变量_id;private string _id;
属性中写代码应该这样写:
private string _id;
public string _Id
{
get
{
return _id;
}
set
{
// 验证输入长度小于 2
if (value.Length > 2)//这用的都是Value值
_id = value;//这也应该加上,小于的时候怎么样。
else
_id = "";
}
}

完整的代码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace c5
{
class Program
{
static void Main(string[] args)
{
c5 n = new c5();
n._Id = "123";
n.show();
Console.Read();
}
}
class c5
{
pr