The question is does the value change and do you need to save it back to the registry? Or is the value from registry always correct and never updated?
For the first instance:
The private backing field:
private static HorizontalAlignment? _Alignment;
The property:
public static HorizontalAlignment Alignment
{
get
{
if (_Alignment == null)
{
_Alignment = GetAlignment();
}
return _Alignment.Value;
}
set
{
if (_Alignment != value && SetAlignment(value))
{
_Alignment = value;
OnAlignmentChanged(new AlignmentChangedEventArgs(value));
}
}
}
The "Get" method:
private static HorizontalAlignment GetAlignment()
{
HorizontalAlignment alignmentValue = DEFAULT_ALIGNMENT;
using (RegistryKey registryKey = Registry.LocalMachine.CreateSubKey(REGISTRYKEY))
{
if (registryKey != null)
{
string tempAlignment = registryKey.GetValue(ALIGNMENT_KEYNAME, string.Empty).ToString();
if (!string.IsNullOrEmpty(tempAlignment))
{
try
{
alignmentValue = (HorizontalAlignment)Enum.Parse(typeof(HorizontalAlignment), tempAlignment, false);
}
catch (Exception exception)
{
alignmentValue = DEFAULT_ALIGNMENT;
Logging.LogException(exception);
}
}
}
}
return alignmentValue;
}
The "Set" method:
private static bool SetAlignment(HorizontalAlignment value)
{
bool flag = true;
using (RegistryKey registryKey = Registry.LocalMachine.CreateSubKey(REGISTRYKEY))
{
if (registryKey != null)
{
try
{
registryKey.SetValue(ALIGNMENT_KEYNAME, value.ToString(), RegistryValueKind.String);
}
catch (Exception exception)
{
Logging.LogException(exception);
flag = false;
}
}
}
return flag;
}
If your question is "Is it required to implement a Set accessor?" then the answer is no. The following are also valid.
public int MyInt { get { return 1; } }
public int MyInt { get; protected set; }