Microsoft says fields and properties must differ by more than just case. So, if they truly represent the same idea how should they be different?
Here's Microsoft's example of what not to do:
using System;
namespace NamingLibrary
{
public class Foo // IdentifiersShouldDifferByMoreThanCase
{
protected string bar;
public string Bar
{
get { return bar; }
}
}
}
They give no guidance on how this should look. What do most developers do?
No, Microsoft says publicly visible members must differ by more than just case:
This rule fires on publicly visible members only.
(That includes protected members, as they're visible to derived classes.)
So this is fine:
public class Foo
{
private string bar;
public string Bar { get { return bar; } }
}
My personal rule is not to allow anything other private fields anyway, at which point it's not a problem.
Do you really need protected fields? How about making the property have a protected setter if you want to be able to mutate it from derived classes?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With