call a property on an instance of a class in csharp

To call a property on an instance of a class in C#, you first need to create an instance of the class. Once you have the instance, you can use the dot notation to access the property. Here is an example:

main.cs
// define a class
public class Person 
{
  public string Name { get; set; }
  public int Age { get; set; }
}

// create an instance of the class
Person person1 = new Person();

// set the values of the properties
person1.Name = "John";
person1.Age = 30;

// access the properties
string name = person1.Name;
int age = person1.Age;
331 chars
18 lines

In this example, we define a class Person with two properties Name and Age. We then create an instance of the class using the new keyword. Once we have the instance, we can set the values of the properties using the dot notation. Finally, we can access the properties using the same dot notation.

gistlibby LogSnag