Przekazuj wartości które Cię interesują jako argumenty w metodach, konstruktorach itp
public class Person
{
public Person(string Name, string Surname, string Age)
{
this.Name = Name;
this.Surname = Surname;
this.Age = Age;
}
public string Name { get; private set; }
public string Surname { get; private set; }
public string Age { get; private set; }
}
public void Metoda1()
{
Person person1 = new Person("Jan", "Kowalski", "21");
Person person2 = new Person("Wladimir", "Kowalski", "21");
Person person3 = new Person("Karol", "Kowalski", "21");
Metoda2(person1); // wyświetli Jan
Metoda2(person2); // wyświetli Wladimir
Metoda2(person3); // wyświetli Karol
}
public void Metoda2(Person person)
{
Console.WriteLine(person.Name);
}
Jeżeli tworzysz obiekty o zasięgu globalnym zadbaj o to, aby referencja do obiektu miała odpowiedni modyfikator dostępu w zależności od tego w jaki sposób ją wykorzystujesz