It is not int and not string. It's clearly written that this is a function. The question does not make any sense at all.
And the implementation of the method makes no sense at all.
You create two instances of Person. Then you loose them, because they are local variables. The frame stack is removed after the call, with all your variables. The actually created objects become inaccessible and will be eventually removed by GC.
This is something you have to understand:
Garbage collection (computer science) — Wikipedia, the free encyclopedia[
^].
"What to do" would not be a correct question. It depends on what you want to achieve. I would suggest that, in the give context, the whole idea to create a method (static or not) creating two instances of the same class makes no practical sense at all. Let's see. First of all this is how you can return these to objects in the simplest possible way:
public static Person[] PersonData() {
return new Person[] { new Person(), new Person(), };
}
Another option would be:
Tuple(T1, T2) Class (System)[
^]
But why? Why?!
Let's consider the calling code using this method. What can it do?
Person[] people = PersonData();
people[0].DoSomething();
people[1].DoSomething();
(new Person()).DoSomething();
Here is the deal: it all makes no sense, because the method
PersonData
does not create any value, does not introduce any knowledge. This is the main thing. Avoid such methods.
(And of course it makes no sense to create a
List
in such function. A list is good if you have several different calls with the same list, but then the list instance should be transparently passed to method as a parameter. Solution 1 makes not much more sense than your question.)
See also:
Poltergeist (computer programming) — Wikipedia, the free encyclopedia[
^],
https://en.wikipedia.org/wiki/Cargo_cult_programming[
^],
https://en.wikipedia.org/wiki/Spaghetti_code#Lasagna_code[
^].
—SA