1

I have a base class:

abstract class foo(){
     abstract dynamic DefaultValue { get; }
}

I'd like to be able to inherit from this for multiple objects and set a default value:

class bar : foo{
     override dynamic DefaultValue { get { return 0.0f; } }
}

How would this be represented in memory? would each object store its own "DefaultValue", or would it only be stored in memory once with each object referenciong it?

David Torrey
  • 1,335
  • 3
  • 20
  • 43

3 Answers3

1

It will make sense to create a method for the DefaultValue something as :

GetDefaultValue(){ return 0.0f; }

As long as methods do not take additional memory per object. On the other side, declaring auto-properties, will always create additional private fields to hold the data, and this will be added on each object you create, so it's not memory efficient.

It is realy a confuzing situation, as looking at this question will see that an auto property will take additional space.

But if you look at Jon Skeet's answer right here, he is saying that

The size of an individual object is not affected by the number of properties, methods, events etc.

In this case they may have the same effect, you can test it yourself against a large range of objects, but it is more surely to make a method, as it is referenced by all the objects within the method table.

Community
  • 1
  • 1
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • I think that answers my question. I'd like it to obviously be more memory efficient, it just feels more clean in terms of working with it to make it a property. – David Torrey Jul 10 '16 at 20:21
  • 1
    @DavidTorrey you don't have to worry about the memory of a single property, what you should is the design of your application, and the code to be clean. Because this is a managed environement, rarely will be the case when the memory will ever bother you. – meJustAndrew Jul 10 '16 at 20:33
1

Short answer - there will be no extra memory per object to store a default value A read only property with only a return statement will compile to a method

get_DefaultValue()
Eric Linde
  • 191
  • 6
  • so as long as the property doesn't reference an internal variable you're saying the compiler underneath converts it to a method automatically? – David Torrey Jul 10 '16 at 20:25
  • 1
    The compiler always converts a property to one or two metods (one for get and one for set) in this case your only have one method (get). Storage is only allocated if you have an explicit backing field or if you have an auto property (implicit backing fields) for example `public string Name {get;set;}` – Eric Linde Jul 10 '16 at 20:28
  • that's right, that makes sense. in other words, i'm safe keeping this as a property. if it were left to the default getter and setter it creates an internal field, correct? – David Torrey Jul 10 '16 at 20:29
  • Yep you can keep it as a property. A get+set auto property on the other hand (see my edited comment above) compiles to two methods and a backing field. – Eric Linde Jul 10 '16 at 20:32
0

If that's the case, then I would say make it a generic method like

public T GetTypedefaultvalue<T>()
{
  return default(T);
}

You can then call it as

GetTypedefaultvalue<double>();

(OR)

GetTypedefaultvalue<string>();
Rahul
  • 76,197
  • 13
  • 71
  • 125