所以我试图在C#上使用get/set属性,但我无法让我的代码工作(它崩溃我的控制台应用程序)
这是我的textHandler.cs文件,你可以看到public static void方法WriteInfo正在使用get/set属性但它崩溃了我的应用程序..
class TextHandler
{
public static void WriteInfo(String text)
{
var consoleText = new Text();
consoleText.text = text;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(consoleText);
Console.ForegroundColor = ConsoleColor.White;
}
public static void WriteError(String text)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(text);
Console.ForegroundColor = ConsoleColor.White;
}
public static void WriteSuccess(String text)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(text);
Console.ForegroundColor = ConsoleColor.White;
}
public static void WriteText(String text, ConsoleColor color)
{
}
}
public class Text
{
public String text
{
get
{
return this.text;
}
set
{
this.text = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我称之为方法
TextHandler.WriteInfo("New client with version : " + message + " | current version : " + version);
Run Code Online (Sandbox Code Playgroud)
如果我删除该行应用程序不再崩溃,不知道我做错了什么因为我没有得到任何错误.如果这是一个不好的方法,请告诉我,我想改善谢谢
创建无限递归的代码是:
public String text
{
get
{
return this.text;
}
set
{
this.text = value;
}
}
Run Code Online (Sandbox Code Playgroud)
在集合中,您可以分配this.text = value给自己,从而创建无限递归StackOverflow.
似乎您不需要字段,因此请将代码更改为:
public String Text {get;set} //PROPERTIES ARE UPPERCASE BY MS STANDART
Run Code Online (Sandbox Code Playgroud)