Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

drstrangequark
drstrangequark
8,273 Points

How do you call a Get on a property in C#?

  • I understand that you create "get" and "set" methods when setting up the property. But what is the syntax when you actually want to call the "get" method somewhere else in the code? For example:

public int SomeProperty { get; }

Class class = new Class();

  • Would you get the SomeProperty property from the Class object by writing:

return class.SomeProperty ?

OR

return class.GetSomeProperty ?

OR

something else entirely?

  • I hope that question makes sense.

1 Answer

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hi Sam,

That's a great question. When you declare a property getter and/or setter, you access that property externally by simply using the name of the property itself.

instanceA.SomeProperty;           // Calls the getter
instanceA.SomeProperty = "Hello"; // Calls the setter

If I misunderstood your question please let me know.

I hope this helps.

drstrangequark
drstrangequark
8,273 Points

Nope, I think you answered it. I'm just stuck on a code challenge and thought I was calling the property correctly but I may not be.

Thanks!