using System; class Program { static void Main(string[] args) { Person person1 = new Person("John", 30); Person person2 = new Person("Jane", 25); person1.SayHello(); person2.SayHello(); Console.WriteLine(person1.Name + " is " + person1.Age + " years old."); Console.WriteLine(person2.Name + " is " + person2.Age + " years old."); } } class Person { public string Name { get; set; } public int Age { get; set; } public Person(string name, int age) { Name = name; Age = age; } public void SayHello() { Console.WriteLine("Hello, my name is " + Name + "!"); } }This example defines a
Person
class withName
andAge
properties and aSayHello()
method, creates twoPerson
objects, and accesses their members. The following steps are performed:
- Define a
Person
class withName
andAge
properties and aSayHello()
method.- Create two
Person
objects using thenew
keyword and passing in the name and age as arguments to the constructor.- Call the
SayHello()
method on bothPerson
objects to output a greeting to the console.- Access the
Name
andAge
properties of bothPerson
objects and output them to the console usingConsole.WriteLine()
.Note that there are many other class-related concepts and operations you can perform, such as inheritance, interfaces, and encapsulation. This example is just one possible multi-step process for working with classes.