I have a very simple Color
class implemented below:
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;
}
}
}
}
I have a property called GreyScale which simply returns the average of the red, green and blue fields as a float. However, when I execute the following code, once the GreyScale
property is called the first time, subsequent changes to writeable class members doesn't change the value of GreyScale once it's called again.
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.