The best way for memory to pass method parameters

I'm having memory performance issues with my code, and I'm not sure if this is the best way to pass parameters to my methods. I will give a brief example about this.

I have, for example, 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 use it as follows:

    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 application, I have many method calls, where the whole object (an object with a lot of properties, not like this “Personality” object from my example) was passed as a parameter, and I think to improve the code by sending methods only those the properties they need, but I'm currently not sure how this will improve memory performance.

, Peron , CalculateAge(person);, , CalculateAge(person.DateOfBirth);?

, CalculateAge(person.DateOfBirth); ( ) , , , .

+4
2

:

  • class class
  • struct class
  • class/struct vs. class

: string Name , Person person.

struct, DateTime, , struct. DateTime ( ) - . , . a struct short, . struct s, . DateTimeOffset, .

, class. - struct s.

API, .

Person API, , API Person. , DOB Person API.

age-computing API Person, Person . , Person,

, Age-computing API (, ..), DOB.

+4

, , . , , , 8 64- (. ).

, (n 8 ), " ".

EDIT: @Bashn , DateTime . , DateTime 8 . .

, . ?

+1

Source: https://habr.com/ru/post/1625392/


All Articles