set block正在执行多次 [英] set block is executing multiple times
本文介绍了set block正在执行多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么我的设置块在我为我的属性赋值时多次执行
我的代码是--------
why my set block is executing multiple times when i assign value to my property
my code is--------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace practice
{
class Program
{
public static int eno
{
get {
return 1;
}
set {
eno = value;
}
}
static void Main(string[] args)
{
Console.WriteLine(eno);
eno = 5;
Console.WriteLine(eno);
Console.ReadLine();
}
}
}
推荐答案
嗯......是的 - 它将...
Well...yes - it will...
public static int eno
{
get {
return 1;
}
set {
eno = value;
}
}
所以
So
eno = 5;
调用setter,它执行:
Calls the setter, which does:
int value = 5;
eno = 5;
哪个调用setter,它执行:
Which calls the setter, which does:
int value = 5;
eno = 5;
哪个调用setter,它执行:
Which calls the setter, which does:
int value = 5;
eno = 5;
...
你可以不要在属性中设置属性的值!
试试这个:
...
You can't set the value of a property from within the property!
Try this:
private static int _Eno
public static int Eno
{
get {
return 1;
}
set {
_Eno = value;
}
}
虽然为什么你从getter返回一个常量值是任何人猜的!
Though why you return a constant value from the getter is anyones guess!
这篇关于set block正在执行多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文