Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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/cmake/2.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# - Fatal编程技术网

更改可写备份字段后,C#中的只读属性永远不会更新

更改可写备份字段后,C#中的只读属性永远不会更新,c#,C#,我在下面实现了一个非常简单的Color类: using System; namespace HelloWorld { internal class Color { private byte red; private byte green; private byte blue; private byte alpha; public Color(byte red, byte green, byte bl

我在下面实现了一个非常简单的
Color
类:

using System;
namespace HelloWorld
{
    internal class Color
    {
        private byte red;
        private byte green;
        private byte blue;
        private byte alpha;

        public Color(byte red, byte green, byte blue, byte alpha)
        {
            this.red = red;
            this.green = green;
            this.blue = blue;
            this.alpha = alpha;
        }

        public Color(byte red, byte green, byte blue)
        {
            this.red = red;
            this.green = green;
            this.blue = blue;
            this.alpha = 255;
        }

        public byte Red { get; set; }
        public byte Green { get; set; }
        public byte Blue { get; set; }
        public byte Alpha { get; set; }



        public float GreyScale
        {
            get
            {
                return (red + green + blue) / 3.0F;
            }
        }
    }
}
我有一个名为GreyScale的属性,它只是以浮点形式返回红色、绿色和蓝色字段的平均值。但是,当我执行以下代码时,一旦第一次调用了
GreyScale
属性,对可写类成员的后续更改不会在再次调用时更改GreyScale的值

Color color = new Color(255, 255, 255, 255);
Console.WriteLine($"{color.GreyScale}"); // prints 255 as expected
color.Red = 197;
color.Green = 197;
color.Blue = 197;
Console.WriteLine(%"{color.GreyScale}");// should print 197. Prints 255.

您只是误解了C#auto属性
{get;set;}
,也就是说,编译器使用getter和setter为公共属性创建私有支持变量。您不需要创建专用字节字段,因此在代码中不会使用它们

using System;
namespace HelloWorld
{
    internal class Color
    {

        public Color(byte red, byte green, byte blue, byte alpha)
        {
            this.Red= red;
            this.Green = green;
            this.Blue = blue;
            this.Alpha= alpha;
        }

        public Color(byte red, byte green, byte blue)
        {
            this.Red= red;
            this.Green = green;
            this.Blue = blue;
            this.Alpha = 255;
        }

        public byte Red { get; set; }
        public byte Green { get; set; }
        public byte Blue { get; set; }
        public byte Alpha { get; set; }



        public float GreyScale
        {
            get
            {
                return (this.Red + this.Green + this.Blue) / 3.0F;
            }
        }
    }
}

您只是误解了C#auto属性
{get;set;}
,也就是说,编译器使用getter和setter为公共属性创建私有支持变量。您不需要创建专用字节字段,因此在代码中不会使用它们

using System;
namespace HelloWorld
{
    internal class Color
    {

        public Color(byte red, byte green, byte blue, byte alpha)
        {
            this.Red= red;
            this.Green = green;
            this.Blue = blue;
            this.Alpha= alpha;
        }

        public Color(byte red, byte green, byte blue)
        {
            this.Red= red;
            this.Green = green;
            this.Blue = blue;
            this.Alpha = 255;
        }

        public byte Red { get; set; }
        public byte Green { get; set; }
        public byte Blue { get; set; }
        public byte Alpha { get; set; }



        public float GreyScale
        {
            get
            {
                return (this.Red + this.Green + this.Blue) / 3.0F;
            }
        }
    }
}

设置属性
红色
绿色
蓝色
时,不会更新字段
红色
绿色
蓝色

如果将
GreyScale
实现为
return(红色+绿色+蓝色)
,它将工作,但只有在设置这些属性之后


我认为您可能希望实现
红色
绿色
蓝色
的getter和setter来读取和写入支持字段。

当您设置属性
红色
绿色
蓝色
时,字段
红色
绿色
蓝色
未更新

如果将
GreyScale
实现为
return(红色+绿色+蓝色)
,它将工作,但只有在设置这些属性之后


我认为您可能希望实现
红色
绿色
蓝色
的getter和setter来读取和写入支持字段。

@Lewsterin:如果是这样,它不会引发
颜色。红色=197
?@Lewsterin我认为您可能需要重新了解属性的工作方式。对于一个方法来说,它的语法糖,它当然可以读取类的当前状态。属性本身没有任何属性state@LewsTherin:不是真正的问题。请参阅下面的答案。我完全忽略了用于计算
灰度值的私人支持字段。区分大小写的C#再次愚弄了我:)@Lewsterin:如果这是真的,它会不会引发
color.Red=197
?@Lewsterin我想你可能需要重新理解属性是如何工作的。对于一个方法来说,它的语法糖,它当然可以读取类的当前状态。属性本身没有任何属性state@LewsTherin:不是真正的问题。请参阅下面的答案。我完全忽略了用于计算
灰度值的私人支持字段。区分大小写的C#再次愚弄了我:)或者干脆完全消除支持字段,他们什么都没做或者干脆完全消除支持字段,他们什么都没做