简体   繁体   English

C#:受保护成员字段的命名规则

[英]C#: naming rules for protected members fields

In our .NET software component we use the following naming convention.在我们的 .NET 软件组件中,我们使用以下命名约定。 When a customer use our DLL from VB.NET, the compiler cannot distinguish distance member field from the Distance property.当客户从 VB.NET 使用我们的 DLL 时,编译器无法区分distance成员字段和Distance属性。 What workaround do you recommend?您推荐什么解决方法?

Thanks.谢谢。

public class Dimension : Text
{
    private string _textPrefix;

    protected double distance;

    /// <summary>
    /// Gets the real measured distance.
    /// </summary>
    public double Distance
    {
        get { return Math.Abs(distance); }
    }
}

You should not use fields that are protected, for the reason that versioning and access cannot be guarded.您不应使用受保护的字段,因为无法保护版本控制和访问。 See the Field Design guidelines.请参阅现场设计指南。 Change your field to a property, which will also force you to change to name (as you cannot have two properties with the same name).将您的字段更改为一个属性,这也会强制您更改为 name(因为您不能有两个同名的属性)。 Or, if possible, make the protected field private.或者,如果可能,将受保护的字段设为私有。

To make setting your property accessible only to the inheriting classes, use a protected setter:要使您的属性设置只能由继承类访问,请使用受保护的 setter:

public class Dimension : Text
{
    private string _textPrefix;

    private double _absoluteDistance;

    /// <summary>
    /// Gets the real measured distance.
    /// </summary>
    public double Distance
    {
        get { return _absoluteDistance  }
        protected set { _absoluteDistance = Math.Abs(distance); }
    }
}

Although that does cause divergence between get and set, as functionality is not the same.尽管这确实会导致 get 和 set 之间出现分歧,但因为功能不一样。 Perhaps a separate protected method would be better in this case:在这种情况下,也许单独的受保护方法会更好:

public class Dimension : Text
{
    private string _textPrefix;

    /// <summary>
    /// Gets the real measured distance.
    /// </summary>
    public double Distance { get; private set; }

    protected void SetAbsoluteDistance(double distance)
    {
        Distance = Math.Abs(distance);
    }
}

Well, summarizing of what already being said you can do something like this :好吧,总结一下已经说过的内容,您可以执行以下操作:

public class Dimension : Text
{
    private string _textPrefix;

    private double _rawDistance;

    /// <summary>
    /// Gets the real measured distance.
    /// </summary>
    public double AbsoluteDistance
    {
        get; private set;
    }

    /// <summary>
    /// Gets the raw distance
    /// </summary>
    public double RawDistance
    {
        get { return _rawDistance; }
        protected set { _rawDistance = value; AbsoluteDistance = Math.Abs(value); }
    }
}

When RawDistance 's value is set it also sets value for AbsoluteDistance and because of that there is no need to invoke Math.Abs() in getter of "AbsoluteDistance".RawDistance的值被设置时,它也会为AbsoluteDistance设置值,因此不需要在“AbsoluteDistance”的getter中调用Math.Abs()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM