To call a public property on an instance of a class in C#, you can use the dot notation. First, create an instance of the class using the constructor, then call the property using the dot notation. For example, consider a class Person
with a public property Name
:
main.cs61 chars4 lines
To create an instance of this class and set the value of Name
, you can use the following code:
main.cs52 chars3 lines
Here, we first create an instance of the Person
class using its default constructor, and then set the value of the Name
property to "John"
. The dot notation (.
) is used to access the property.
To retrieve the value of the Name
property, you can simply use the dot notation again:
main.cs27 chars2 lines
Here, we assign the value of person.Name
to the name
variable. Again, the dot notation is used to access the property on the instance of the Person
class.
gistlibby LogSnag