Tuesday, June 04, 2013

LINQ 04- Auto implemented properties

While writing properties to classes, mostly we declare a variable and expose it through properties.

public class Employee {
    private int id;
    public int ID {
        get { return id; }
        set { id = value; }

Auto implemented properties help to reduce the code by implicitly doing the field declaration, return and assignment through get and set of the properties.


With auto implemented properties, above code can be optimized as

public class Employee {
public int ID { get; set; }


Properties type is used for the implicit variable.

Implicit variable will be declared as private.

Automatically implemented properties must define both get and set accessors

We can use separate access modifier for get or set. (public int ID { get; private set; })

No comments:

Post a Comment