Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value vs reference types when using interfaces in C#

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?

like image 847
brdlph Avatar asked May 30 '13 16:05

brdlph


People also ask

Is an interface a value type or reference type?

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.

Are interfaces reference types?

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.

Which is better value type or 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.

What is the advantage of using a reference type over a value type?

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.


2 Answers

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.

like image 70
Reed Copsey Avatar answered Oct 13 '22 23:10

Reed Copsey


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).

like image 24
Sreejith Nair Avatar answered Oct 13 '22 23:10

Sreejith Nair