문제

이와 같이 간단한 속성을 구성하는 것을 좋아합니다 :

private int foo;
public int Foo
{
    get { return foo; }
    set
    {
        // validate value
        foo = value;
    }
}
.

나는 StyleCop 와 함께 놀아 왔으며, 생성자 후에 필드를 배치하기 위해 나에게 소리 쳤다.이 스타일은 일반적으로 필드가 재산 밖에서 언급되지 않은 한 결코 참조되지 않는 한이 스타일이 허용됩니까?참고 : 나는 개인적인 선호도가 관련되어 있음을 깨닫지 만,이 문제에 관한 일반적인 합의가 있는지 궁금합니다.

도움이 되었습니까?

해결책

Yes, that seems reasonable to me.

Typically I put all the fields and properties at the top, then the constructors, then the methods - but if you want to put them after the constructors, that seems reasonable too.

다른 팁

If your properties are going to be simple data access consider using auto properties:

public int Foo { get; set; }

The compiler will create a private member variable behind the scenes on your behalf.

Specifically to your question, don't put too much stock in tools like ReSharper or StyleCop. Some of the ways they format code, and the things they complain about are truly a matter of preference. I do not put the member variables near their public properties, but I can see how this would be convenient.

May? Since this only affects people on your team, you have to figure out what they think is best and go with that. Style Cop is often a bit... overboard on its recommendations.

I usually put them after the property, as the space above is reserved for documentation.

// placed up here, it looks kinda weird, imho
// private int foo;
/// <summary>
/// The index of the Foo in the <see cref="Bar"/>
/// </summary>
public int Foo
{
    get { return foo; }
    set
    {
        // validate value
        foo = value;
    }
}
private int foo;

It may be a matter of preference, but this seems better than having them mixed with private members.

I usually use a nested region for backing fields inside the properties region since it doesn't interfere with visual studio comments and yet they are grouped toghether.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top