In programming, particularly in object oriented programming OOP, an object can be defined as an instance of a class. It represents a real world entity or concept within the program. Objects encapsulate both data attributes or properties and behaviors methods or functions that operate on that data.
using System;
namespace ConsoleApp1
{
public class Person
{
// Attributes
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
// Constructor
public Person(string name, int age, string gender)
{
Name = name;
Age = age;
Gender = gender;
}
// Behavior
public void Introduce()
{
Console.WriteLine($"Hi, my name is {Name}, I'm {Age} years old, and I'm {Gender}.");
}
// Override ToString method to print object details
public override string ToString()
{
return $"Name: {Name}, Age: {Age}, Gender: {Gender}";
}
}
public class Program
{
static void Main(string[] args)
{
// Creating objects of Person class with updated parameters
Person person1 = new Person("A", 23, "female");
Person person2 = new Person("B", 23, "male");
// Calling behavior (method) on objects
person1.Introduce(); // Output: Hi, my name is A, I'm 23 years old, and I'm female.
person2.Introduce(); // Output: Hi, my name is B, I'm 23 years old, and I'm male.
// Printing objects using ToString method
Console.WriteLine(person1); // Output: Name: A, Age: 23, Gender: female
Console.WriteLine(person2); // Output: Name: B, Age: 23, Gender: male
}
}
}