更改可写备份字段后,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#再次愚弄了我:)或者干脆完全消除支持字段,他们什么都没做或者干脆完全消除支持字段,他们什么都没做