I would like to create a type in C# with value like semantics. It is immutable and it has a low memory footprint. However, it is mostly going to be accessed via an interface it implements. In this case, a value type would have to be boxed which means that the actual value would have to be copied from the stack to the heap. Therefore I wonder if there is any advantage at all in using a value type (struct) instead of a reference type (class)?
To illustrate my situation, I provide the following example of an interface I
with implementations ReferenceTypeImplementation
and ValueTypeImplementation
:
interface I
{
int GetSomeInt();
}
class ReferenceTypeImplementation : I
{
public readonly int i;
public ReferenceTypeImplementation (int i)
{
this.i = i;
}
public int GetSomeInt()
{
return i*2;
}
}
struct ValueTypeImplementation : I
{
public readonly int i;
public ValueTypeImplementation (int i)
{
this.i = i;
}
public int GetSomeInt()
{
return i*2;
}
}
I would almost exclusively use these type using the interface I
like
I i1 = new ReferenceTypeImplementation(1);
I i2 = new ValueTypeImplementation(1);
Is there any advantage in using ValueTypeImplementation
over ReferenceTypeImplementation
?
Do interface variables have value-type or reference-type semantics? Interfaces are implemented by types, and those types are either value types or reference types. Obviously, both int and string implement IComparable , and int is a value type, and string is a reference type.
In C#, classes and interfaces are reference types. Variables of reference types store references to their data (objects) in memory, and they do not contain the data itself. An object of type Object , string , or dynamic is also a reference type.
Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable.
Yes the main advantage of Reference type will be the ability to work with the exact same Object. Classes are like Object factories, they create the Object and store it and a reference to it in memory. For example lets say we had a Car Class.
Is there any advantage in using ValueTypeImplementation over ReferenceTypeImplementation?
Not if you're going to use it via the interface. As you mentioned, this will box the value type, which negates any potential performance improvement.
In addition, the fact that your expected main usage is going to be via the interface would suggest that you're expecting reference semantics, which again suggests that a class
would be more appropriate in this case.
Converting a struct to an interface causes boxing. Calling an implicitly implemented member on a struct does not cause boxing:
interface I { void Foo(); }
struct S : I { public void Foo() {} }
S s = new S();
s.Foo(); // No boxing.
I i = s; // Box occurs when casting to interface.
i.Foo();
Here in your case, if you can work with implicit implementation / call then you are better off with struct since your are avoiding;
(a) boxing
(b) no extra memory overhead (which is applicable on any reference type allocation).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With