vexe
vexe

Reputation: 5615

Do properties/methods in classes take space in memory?

If we had code like this:

public class Enemy
{
   public int hp;
}

Then an Enemy object would take 4 bytes in 32-bit machines, and 8 bytes in 64-bit (correct me if I'm wrong).

If we change it to something like this:

public class Enemy
{
   public int hp;
   public void Attack() {}
}

An Enemy object would still take the same amount of memory as it did before, right?

The same for this:

public class Enemy
{
   private int hp;
   public int Hp { get { return hp; } set { hp = value; } }
}

From what I know, a property is function, but treated as a variable, right?

So if we did this:

public class Enemy
{
   public int Hp { set; get; }
}

Does that mean, an Enemy object now takes no memory space at all? That doesn't make any sense.

Or even this, for that matter:

public class Enemy
{
   public void DoSomething() { }
}

Can somebody explain?

Upvotes: 4

Views: 6255

Answers (3)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391406

int in C# is always going to be System.Int32 which will always take up 4 bytes of space, regardless of 32-bit or 64-bit application.

However, there's additional overhead in an object. Jon Skeet has a blogpost that details some of this here, Of memory and strings.

As you can see, the base size of an object is 12 bytes when running as 32-bit, even if you have no fields.

You're right, however, in that a property that has code does not necessarily increase the size of the object.

However, if you make it an auto-property, like this:

public int Hp { get; set; }

Then the compiler will automagically create a backing field for you to hold the value of that property, which will again take up space.

Upvotes: 6

Iłya Bursov
Iłya Bursov

Reputation: 24146

public int Hp { set; get; } means - AUTOgenerate variable, and its access methods, so this variable will take space, the same as you manually define it.

more details:

your code is compiled into any kind of binary code (either pure machine codes, or byte code) - so, your code is always occupy some memory during storing on HD or during running in RAM, also - for each instance of any class, during runtime additional memory for variables of this class is allocated, so all your classes take memory, classes without variables use almost 0 additional memory for each instance, but there are can be some "technical" data for each instance, like vrtbl, so usually - all classes and all objects takes some memory

Upvotes: 2

Jeroen van Langen
Jeroen van Langen

Reputation: 22038

public int Hp { set; get; } Is an auto-implemented property, a private field will be generated at compile-time.

like:

public class Enemy
{
   public int Hp { set; get; }
}

Will be implemented as.

public class Enemy
{
   private int _hp;

   public int Hp 
   { 
       get { return _hp; }
       set { _hp = value; }
   }
}

Also adding extra method will use extra memory, but not per-instance. Look: Where are methods stored in memory?

Upvotes: 1

Related Questions