Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I understand somehow the get and set in c#.
But I did not understand the simple one like:

C#
public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
}


what is the advantage of it compared to the one without get and set, namely:

C#
public class Customer
{
    public int ID;
    public string Name;
}


At least, from the calling no any difference although the mechanism behind is different. Anyone can clarify me the concrete advantage by using simple get and set? or give me a concrete exmaple?


I know that above simple version is equivalent to the following.
C#
public class Customer
{
    private int m_id = -1;

    public int ID
    {
        get
        {
            return m_id;
        }
        set
        {
            m_id = value;
        }
    }

    private string m_name = string.Empty;

    public string Name
    {
        get
        {
            return m_name;
        }
        set
        {
            m_name = value;
        }
    }
}
Posted
Updated 2-Sep-12 5:54am
v2
Comments
Seraph_summer 2-Sep-12 15:26pm    
thanks a lot to all of you!!
I love the codeproject website and I love you too!

What you're referring to is Auto-Implemented properties[^]

Basically they help you to define a property without the complexity of defining a class variable where to hold the data.

The other thing you asked was why to use auto-implemented properties and not simply fields. This is a design question and commonly it's considered a bad practice to define fields public to be modified outside the class.

Another thing is that you can define access levels for autoimplemented properties. For example you can define the following
C#
public class Customer
{
    public int ID { get; private set; }
    public string Name { get; set; }
}

That would mean that the ID can only be set from inside the class.
 
Share this answer
 
Comments
Mehdi Gholam 2-Sep-12 12:02pm    
5'ed
Wendelius 2-Sep-12 12:15pm    
Thanks Mehdi :)
Manas Bhardwaj 2-Sep-12 13:30pm    
Right +5!
Wendelius 2-Sep-12 13:48pm    
Thanks Manas :)
Sergey Alexandrovich Kryukov 11-Jun-13 22:52pm    
5ed.
—SA
C#
...{
    public string Name {get;set;}
}

Is a compiler generated back end field get/set (as opposed to defining your own fields - a short hand for writing code faster).
C#
...{
    public string Name;
}

Is a field access definition.
You need get/set for .net data binding to work (as by default it does not bind to field properties in UI components like the grid etc.).

Generally it is better to have get/set since you can do checking and validation if it is needed in the future and the code using this will not change [fields need a recompile].
 
Share this answer
 
Comments
Wendelius 2-Sep-12 12:15pm    
Good points, 5'd.
Mehdi Gholam 2-Sep-12 12:20pm    
Cheers Mika!
Congratulations on your sixth platinum!
Wendelius 2-Sep-12 13:49pm    
Oh, thanks! Looks like I'm having too much spare time... :)
Mehdi Gholam 2-Sep-12 14:02pm    
We all suffer, no cure in sight! :)
Wendelius 2-Sep-12 14:04pm    
:-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900