Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!