1

Someone once wrote:

The space required for an instance depends only on the fields.

The methods require memory too but only one time per class. Like static fields. That memory is allocated when the class is loaded.

But what happens if a class with say like 5 methods and no fields get multiple instances in fields of other classes(composition). Do they require more memory? Or would it be the same as static methods?

I do ask this question also because maybe it even gets optimised when compiling?

Is there a differents to static class with static methods? Other than u need to create the class each time or pass it around?

Eg.:

class Test1
{
    public void DoThis()
    {
        ...
    }

    public void DoThat()
    {
        ...
    }
}

class Test2
{
    public void DoSomething()
    {
        ...
    }

    private Test1 sample = new Test1();

}

class Test3
{
    public void DoSomethingElse()
    {
        ...
    }

    private Test1 sample = new Test1();

}

And so on...
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
MIkey
  • 21
  • 1
  • 2
  • 1
    who is "someone" ? – Leandro Bardelli Dec 12 '21 at 19:18
  • It was a question on stackoverflow somewhere, but i cannot seem to find it anymore. It had like 5 upvotes or so so I assume it is or was somewhat true. But this has little to do with the question here. – MIkey Dec 12 '21 at 19:22
  • 1
    Once again not the question but I am asking this mainly because in a larger scale than this it would have a impact IF there would be more memory consumption. – MIkey Dec 12 '21 at 19:25
  • 1
    @It is not so simple. For the start you need to select a good text book and learn by heart hundreds pages about the memory management if you want to understand how it works. – Serge Dec 12 '21 at 19:33
  • @Serge So I assume u mean that there is no answer to this question that can be answered without reading hundred of pages? If so Thank you for your time. Also can u maybe recommend a good C# book what is fairly modern and or good? (In your opinion) – MIkey Dec 12 '21 at 19:39
  • @MIkey I am sorry if your don' t understand what I mean, but my point is that you should think and ask this kind of question after you create 10-20 applications. Trust me what are you asking for it is not the main problem to create a good application. There are alot much more important topics you should start from. – Serge Dec 12 '21 at 19:43

1 Answers1

0

"Behind the scenes", a class method is just like a static method, with the class instance beeing passes by reference as the first parameter.

That is, unless you use virtual methds, which "behind the scenes" are saved as instance members.

That is, because as long as you don't override a method, there is simply no reason to waste an instance's space.

Therefore, the size of both your class instances won't be affected by any non-virtual method you add to the class.

This concept can change between programming languages tho. For example, in Java and Python class methods are virtual by default.

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
aviad1
  • 354
  • 1
  • 10
  • well it's definitely not false. I can only assume that someone thought I wasn't beeing exact enough or something, or maybe didn't see what language you were asking about. But this is a general description of how class methods work in C#. – aviad1 Dec 12 '21 at 19:39
  • @aviad1 you should read the guide on how a question is useful and how an answer is useful. The question should be vote to closed, don't be answered to obtain points. This is not a game – Leandro Bardelli Dec 12 '21 at 19:40
  • Neither you should upvote it. – Leandro Bardelli Dec 12 '21 at 19:40
  • 1
    @LeandroBardelli I didn't upvote the question. And I don't collect points. I've only started answering questions on this site. I saw a genuine quesion about memory usage, and I tried to answer it in a brief, logical way, form my knowledge. I'm not some sort of point collector and I'm sorry if I did something wrong. But personally, I think that this is an OOP principle that is important to understand. – aviad1 Dec 12 '21 at 19:51