tags:

views:

136

answers:

3

Hello guys. I know this must be a common question, but take a look:

Here i have a test class:

public class EmployeeClass
{
private int _id;
private string _name;
private double _salary;

public int id
{
get{...}
set{...}
}

public string name
{
get{...}
set{...}
}
//and so on
}

The question is: for me, it doesnt make sense to have public properties matching ALL private fields.

What is the approach to limit the access to itens of the class?

At the time of initialization do i access the fields directly, as in:

public EmployeeClass(int id, string name, double salary)
{
_id = id;
_name = name;
_salary = salary;
}

AND MAKE ALL (at least the ones that must have some sort of immutability) readonly?

What's the best approach here?

Thank you for your opinions

+1  A: 

Eric Lippert has a great series on the different kinds of immutability in c#. He's also got some examples for how to implement the different kinds of immutability.

What he calls "write-once immutability" is the kind where initialization is done in the constructor of the class and all fields are marked as private readonly fields.

dthrasher
+2  A: 

There are many attitudes, two of them that helps avoiding getters on the class are:

In general (and this is only a tiny taste), it is not recommended to have many public setters on the class.

Elisha
I sort of agree but there is this being caveat in that this is a very deeply religious discussion.
BobbyShaftoe
i certainly don't want to start a holy war. But this is making me crazy. I know that there are many approaches, but i don't want to go one way (or another) and later find out that i need to refactor (a lot of) code to make it work. Argh!
George
Elisha
great comment Elisha. Well, first of all, i want to code better. That is definitely a major reason for me. I will read all links that you have passed along, and will give it a try. East orientation seems to me very nice and i will try to go that way.Thanks
George
Agree with Elisha's point about public setters. However, it's worthy to note that the decision also involves considerations of what frameworks you are using, as their conventions may require public setters (or other design elements). For example, ASP.NET MVC default model-binding (and hence validation) requires public setters for mutable data. There are ways around it, but fighting conventions is cumbersome to say the least.
gWiz
+2  A: 

To answer the question from a language convention point of view, prior to C# 3.0, access to internal data was typically performed by declaring private fields with public properties providing read and/or write accessors:

public class Employee
{
    private int _id;

    public int Id
    {
        get { return _id; }
        set { _id = this; }
    }
}

Note: In .Net, properties are in PascalCase by convention.

While the purpose of properties are to provide encapsulation around accessing an object's internal state, properties are often used merely as a pass through to private field access as a placeholder in the event additional behavior is needed in the access or computation of the state. Because this pattern leads to a lot of verbose and repetitive coding, Microsoft introduced the Auto-Implemented Properties feature in C# 3.0. Auto properties allow the developer to declare properties which are automatically backed by a private variable:

public class Employee
{
    public int Id { get; set; }
}
Derek Greer
Upvote because I love using the new 3.0 properties with C#. One my university professor claims that using these type of properties is incorrect and spaghetti code. Go figure.
Sergio Tapia