我試圖使用條件語句來檢查值是否等於使用setter
進行設置之前的值。我返回了一個StackOverFlowException錯誤。我正在使用另一個.cs文件來執行一般練習的代碼。設置屬性時出現StackOverFlowException
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Job_Classes
{
class Workers
{
public string Name
{
get { return Name; }
set
{
if (Name.CompareTo("Admin") == 0 || Name.CompareTo("Admin") == -1) //Just trying out comparison with the input.
{
Console.WriteLine("Invalid Name."); //To see if an invalid input that is not "Admin" fails.
}
else
{
Name = value;
Console.WriteLine("Done.");
}
}
}
public Workers()
{
this.Name = null;
}
public Workers(string Name)
{
this.Name = Name;
}
public string Information()
{
return String.Format("Name: {0}", Name);
}
}
}
在其他的.cs我執行的代碼是:
Workers Test = new Workers("John");
讀取異常跟蹤;它會顯示重複調用.. – user2864740