Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重写虚拟属性时如何避免无限循环?_C#_.net - Fatal编程技术网

C# 重写虚拟属性时如何避免无限循环?

C# 重写虚拟属性时如何避免无限循环?,c#,.net,C#,.net,在分配或检索值时是否可以忽略set和get 具体来说,我是从一个类继承的,该类的属性声明如下: virtual public Int32 Value { get; set; } override public Int32 Value { get { return this.Value; } set { this.Value = value; // do something useful } 我想做

在分配或检索值时是否可以忽略set和get

具体来说,我是从一个类继承的,该类的属性声明如下:

virtual public Int32 Value { get; set; }
override public Int32 Value
{
    get
    {
        return this.Value;
    }
    set
    {
        this.Value = value;
        // do something useful
    }
我想做的是重写它,并在那些set和get中做一些有用的事情。当我重写它时会出现问题,我还必须手动指定或从属性返回值。如果我这样做:

virtual public Int32 Value { get; set; }
override public Int32 Value
{
    get
    {
        return this.Value;
    }
    set
    {
        this.Value = value;
        // do something useful
    }

然后我创建一个无限循环。有没有一种方法可以在不调用set和get中的代码的情况下设置或获取值,或者我必须为实际变量指定一个单独的名称?

而不是使用
this.value
,您应该使用
base.value
。它将检索/设置基类中的属性


请注意,基本方法实际上必须是可重写的(
virtual
abstract
);在你的例子中不是这样的。如果基方法不是虚拟的,那么当您尝试在派生类中重写时,只会得到一个编译器错误。

您要查找的关键字是
base
。这迫使编译器将属性引用解析为基类中定义的属性引用。与VB.NET等效的是MyBase

因此:


嗯,它实际上是虚拟的,我只是忘了在这里输入,非常感谢。