这很容易解释:这很有效
using System;
using ConstraintSet = System.Collections.Generic.Dictionary<System.String, double>;
namespace ConsoleApplication2
{
class test
{
public ConstraintSet a { get; set; }
public test()
{
a = new ConstraintSet();
}
static void Main(string[] args)
{
test abc = new test();
Console.WriteLine("done");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不
using System;
using ConstraintSet = System.Collections.Generic.Dictionary<System.String, double>;
namespace ConsoleApplication2
{
class test
{
public ConstraintSet a { get { return a; } set { a = value; } }
public test()
{
a = new ConstraintSet(); …Run Code Online (Sandbox Code Playgroud) 这是我的代码.我无法弄清楚为什么这段代码给出了'由于StackOverFlowException而导致进程终止'.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SadiSDAL4
{
class Program
{
static void Main(string[] args)
{
PrivateVehical privateV = new PrivateVehical("001");
PrivateVehical pVehical = (PrivateVehical)privateV.Clone();
Console.WriteLine("Cloned : {0}", privateV.name);
Console.WriteLine("Cloned : {0}", pVehical.name);
privateV.name = "Sadia's Car";
Console.WriteLine("Cloned : {0}", privateV.name);
Console.WriteLine("Cloned : {0}", pVehical.name);
pVehical.name = "Waheed's Car";
Console.WriteLine("Cloned : {0}", privateV.name);
Console.WriteLine("Cloned : {0}", pVehical.name);
Console.WriteLine(privateV.GetHashCode().ToString());
Console.WriteLine(pVehical.GetHashCode().ToString());
PublicVehical publicV = new PublicVehical("002");
Console.WriteLine(publicV.id);
PublicVehical pubVehi = (PublicVehical)publicV.Clone();
Console.WriteLine("Cloned : {0}", pubVehi.id);
} …Run Code Online (Sandbox Code Playgroud) 为什么这个?这是我的代码:
public class KPage
{
public KPage()
{
this.Titolo = "example";
}
public string Titolo
{
get { return Titolo; }
set { Titolo = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
我通过构造函数设置数据.所以,我想做些喜欢的事情
KPage page = new KPage();
Response.Write(page.Titolo);
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个错误:
set { Titolo = value; }
Run Code Online (Sandbox Code Playgroud) 星期五,当我收到堆栈溢出错误时,我正在处理应用程序的公共注释部分,这让我很困惑,所以我想我会请求帮助.使用"堆栈溢出"这个表达式搜索网页有点弄巧成拙!
我想在发送要添加到数据库的类的实例之前,在类的字段的set语句中执行HtmlEncode:
public class Feedback
{
public Feedback() { }
public string FeedbackComment
{
get { return FeedbackComment; }
set {System.Web.HttpUtility.HtmlEncode(value); }
}
// other fields
// methods
}
Run Code Online (Sandbox Code Playgroud)
这导致了StackOverflow错误,我通过将代码更改为如下所示来修复错误:
public class Feedback
{
public Feedback() { }
private string feedbackComment;
public string FeedbackComment
{
get { return feedbackComment; }
set { feedbackComment = System.Web.HttpUtility.HtmlEncode(value); }
}
// other fields
// methods
}
Run Code Online (Sandbox Code Playgroud)
但我只想解释为什么第一个get/set语句是如此递归以至于它们导致堆栈溢出但是当将代码恢复为更像c#2.0时?这可以通过更短的语法来实现,如果是这样的话怎么样?
这是我的第一个问题 - 请尽量保持温和!
class Program
{
static void Main(string[] args)
{
something s = new something();
s.DoIt(10);
Console.Write(s.testCount);
}
}
class something
{
public int testCount
{
get { return testCount; }
set { testCount = value + 13; }
}
public void DoIt(int val)
{
testCount = val;
}
}
Run Code Online (Sandbox Code Playgroud)
是我拥有的,因为我想测试和玩C#的getter/setter东西.但是,我得到一个StackOverFlowException未处理"set {testCount = value + 13}".我无法单步执行它,因为我得到一个"调试器无法继续运行进程.进程已终止"来自Visual Studio的消息.我有什么想法我做错了吗?
编辑:今天我了解到我做了一个非常愚蠢的derp.鉴于大量的即时反应.现在我知道的更好.
我正在编写一个代码,用于在C#中设置属性并获得异常.
public class person
{
public string name
{
set
{
name = value;
}
get
{
return name;
}
}
public static void Main()
{
person p = new person();
p.name = "Bilal";
Console.WriteLine(p.name);
}
}
Run Code Online (Sandbox Code Playgroud)