简体   繁体   中英

How should I use Properties in C#?

Im not sure because in Java getter/setter are looking a little bit different but whats the "c# way" to code this stuff?

Option a.)

    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private int time;

    public int Time
    {
        get { return time; }
        set { time = value; }
    }

b.)

    private string _name;
    private int _time;

    public string name
    {
        get { return _name; }
        set { _name = value; }
    }

    public int time
    {
        get { return _time; }
        set { _time = value; }
    }

c.)

   public string name {get; set;}
   public int time {get; set;}

Ok there are some examples. What would look better? Should I write all the private variable declarations first then the properties or should I group my variable and property declaration next to each other.

How about d, following .NET naming conventions:

public string Name { get; set; }
public int Time { get; set; } // Odd type for time, admittedly...

Don't bother writing the property manually yourself unless you do something non-trivial in it.

If you do write the property implementation manually, it's up to you how you name the private variable. Personally I'd use:

private string name;
public string Name
{
    get { /* whatever */ }
    set { /* whatever */ }
}

... but if you want to use underscores, that's your prerogative.

As for ordering of members - that's even more your own choice. Assuming you're working with a team, talk with the team and see what the local convention is.

If you don't need access to the underlying backing fields, then auto-properties are the suggested way which is (C). However, according to .NET naming convention they should be PascalCase.

public string Name { get; set; }
public int Time { get; set; }

The first two are just naming convensions that you should choose based on the company/dev group or your own decision.

The third case is the short way of decalring the same property , where the actual field will be generated by runtime itself.

Short Pros: it's short and easy .

Short cons: Can not put something inside geter/setter, can not put breakpoint in Visual Studio ...

Hope this helps.

In your specific case (no logic on getter or setter) the best option is C (with a small change on properties names to follow C# standards)

 public string Name {get; set;}
 public int Time {get; set;}

取决于这种情况,当您定义字段时使用下划线字符,如果您只需要一个简单的getter / setter,则无需自己定义字段,因为编译器将在后台为您定义它们。

I would go for the option, most others already posted.

public string Name {get; set;}
public int Time {get; set;}

Note, that you can make change protection-level on the setter and getter individually, eg

public string Name {get; protected set;}
protected int Time {get; private set;}

You can only use a higher protection than applied to the hole property.

public string Name {get; set;}
public int Time {get; set;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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