English 中文(简体)
如何避免使用同一类名称和产权名称的标识?
原标题:How to avoid using the same identifier for Class Names and Property Names?

这里有几个类型和特性具有相同识别特征的例子:

public Coordinates Coordinates { get; set; }
public Country Country { get; set; }
public Article Article { get; set; }
public Color Color { get; set; }
public Address Address { get; set; }
public Category Category { get; set; }

随着实体框架使用关系的财产名称,在使用实体框架的POCO时,这一问题更经常发生。

因此,需要做些什么? 使用非标准类别名称?

public ClsCoordinates Coordinates { get; set; }
public ClsCountry Country { get; set; }
public ClsArticle Article { get; set; }
public ClsColor Color { get; set; }
public ClsAddress Address { get; set; }
public ClsCategory Category { get; set; }

Yuk

或者使用更多的描述性财产名称?

public Coordinates GeographicCoordinates { get; set; }
public Country GeographicCountry { get; set; }
public Article WebArticle { get; set; }
public Color BackgroundColor { get; set; }
public Address HomeAddress { get; set; }
public Category ProductCategory { get; set; }

不理想,但可以与我共同生活。

或掌握信息技术?

你们的最佳做法是什么?

最佳回答

这有时被称为“科罗尔”问题,我的建议只是要与它共存。

对C#语言的具体说明是为了不成为问题。 C第3号第7.5.4.1节:

In a member access of the form E.I, if E is a single identifier, and if the meaning of E as a simple-name (§7.5.2) is a constant, field, property, local variable, or parameter with the same type as the meaning of E as a type-name (§3.8), then both possible meanings of E are permitted. The two possible meanings of E.I are never ambiguous, since I must necessarily be a member of the type E in both cases. In other words, the rule simply permits access to the static members and nested types of E where a compile-time error would otherwise have occurred.

(以实例为依据)

很明显,当你can<>/em>提供描述性更强的财产名称时,这种名称是巨大的,但常常是真实的最佳名称is与财产相同。

在框架本身,例如<代码>HttpWebRequest.CookieContainer为CookieContainer,并有各种类型的。 证据 类型<代码>的财产

问题回答

我试图使用更多的描述性财产名称。

改变阶级名称的感觉就好了,因为大多数开发商往往在使用良好和描述性可变/财产名称的情况下这样做。

比如,正如你一样,地址可以扫描

public Address HomeAddress { get; set; } 
public Address PostalAddress { get; set; } 
public Address CompanyAddress { get; set; } 

等等。 你们可以看看一看这方面的情况。

如果姓名真正适用,我个人不会对姓氏和财产名称相匹配。 例如,在地址类别中,有一个名为Country的财产被归类为“Country的类别,这具有意义,而且对于无多余的财产,实际上没有任何名称。 我试图避免使用描述性财产名称的碰撞,但有时财产的名称及其类型是最好的名称,使用任何其它东西会减少清晰度,而不是加以改进。

我强烈建议不要在课堂上设置类似于匈牙利的预设办法。 这只是显而易见的。

我认为,财产的名称应当只是贬低它。 当它是一种 Apple果时,它只是一个 Apple果。

为什么任何人,就财产的可读性而言,都会以阶级的名义使用 h。 它减少了类别名称的可读性。

在获得财产时,你可以使用这一关键词,防止突然将财产作为一类:

class Food
{
    public Apple Apple
    {
        get;
        set;
    }

    public void DoIt()
    {
        this.Apple = new Apple();
    }
}

class Apple
{
}

见SA1101型式Cop “向当地成员发出呼吁的前提条件是这样做。 注





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...