Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Following is the way I found that is used to create getter and setter in C#.
My question is how can I access getter and setter methods from instance.
C#
class A
{
    public int id { get; set; }
};

I have tried following but id does not work.
A a = new A();
a.getid();
Posted
Comments
BillWoodruff 14-Dec-13 11:34am    
I think it's a very good thing to keep .NET's "dot notation" in mind: any time you want to access, or reference, something inside another object/class/form/container/library of methods, just think of the outer "whatever" followed by a dot, and then name of the "inner" whatever.

And, in your development environment, just typing a name followed by a dot should pop-up a list of the elements exposed in that "whatever." So you can use the IDE to really educate yourself.

1 solution

It's simpler than that:
C#
A a = new A();
int id = a.id;
a.id = 666;
The syntax you are talking of is called an autoimplemented or automatic property, and it's just a short form for:
C#
private int _id;
public int id { get { return _id; } set { _id = value; }}
The compiler generates the _id backing field.

BTW: The standrad is to use and initial uppercase letter foe properties, so your code should really be:
C#
class A
{
    public int Id { get; set; }
}
...
    A a = new A();
    int id = a.Id;
    a.Id = 666;
...
 
Share this answer
 

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