call a public property on an instance of a class in csharp

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.cs
public class Person {
    public string Name { get; set; }
}
61 chars
4 lines

To create an instance of this class and set the value of Name, you can use the following code:

main.cs
Person person = new Person();
person.Name = "John";
52 chars
3 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.cs
string name = person.Name;
27 chars
2 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