I am experiencing some memory performance issues with my code and I am not sure which is the best way to pass parameters to my methods. I will give a short example about this.
I have for instance this class:
class Person
{
public string Name { get; set; }
public bool IsMale { get; set; }
public string Address { get; set; }
public DateTime DateOfBirth { get; set; }
}
and then I am using it like this:
static void Main(string[] args)
{
Person person = new Person {
Name = "John",
Address = "Test address",
DateOfBirth = DateTime.Now,
IsMale = false };
int age = CalculateAge(person);
int age2 = CalculateAge(person.DateOfBirth);
}
private static int CalculateAge(Person person)
{
return (DateTime.Now - person.DateOfBirth).Days;
}
private static int CalculateAge(DateTime birthDate)
{
return (DateTime.Now - birthDate).Days;
}
Now in my app I have lots of method calls where the entire object (an object with lots of properties not like this "Person" object from my example) was passed as parameter and I am thinking to improve the code by sending to the methods only the properties that they need, but currently I am not sure how this will improve memory performance.
How is it better regarding memory usage, to send the entire Peron object to my method like CalculateAge(person);
or to send just the property that is used into the method like CalculateAge(person.DateOfBirth);
?
First I thought that calls like this CalculateAge(person.DateOfBirth);
(sending as parameters only the needed properties instead of the entire object) are using less memory but in the end after tests I noticed that app performs slower, and now I am not sure if these changes or others were slowing down my app.