How can a value type implement an interface type?
Asked Answered
L

3

9

In the C# Language Specification v5.0, in section 1.3, it says this:

An Interface type can have as its contents a null reference, a reference to an instance of a class type that implements that interface type, or a reference to a boxed value of a value type that implements that interface type

I have no problem with two out of three of those statements. However, the last one confuses me. How can an interface type hold a boxed value of a value type that implements that interface type? I thought value types couldn't implement interface types? Or is it saying that the boxed value implements the interface type? If that's the case, how can a boxed value implement an interface type?

I'm having a spot of trouble comprehending all of this.

Labiche answered 25/1, 2014 at 0:13 Comment(1)
this is a good question +1 for thisPraise
S
13

Value type (struct) can implement interface. It cannot inherit another struct, but can implement interface.

struct (C# Reference)

Structs can implement an interface but they cannot inherit from another struct. For that reason, struct members cannot be declared as protected.

So when you have a struct which implements IInterface and you do following:

var value = new MyStruct();
var valueAsInterface = (IInterface)value;

valueAsInterface contains reference to a boxed value of a value type that implements that interface type.

Smallwood answered 25/1, 2014 at 0:15 Comment(5)
Could you not say IInterface valueAsInterface = value?Labiche
Yes, you can. Will not make any difference (unless cast operator is overriden in the struct).Smallwood
Why would you have the need to box a value type that implements an interface type? All of this jargon is making my head spin.Labiche
It will be boxed every time you pass it to a method which expects an interface. You can also find it usefull when interface is implemented explicitly.Smallwood
font it use full? What does that mean?Labiche
L
5

There's nothing that says that a value type can't implement an interface.

The following code is perfectly legal:

interface ITest
{
    void DoSomething();
}

struct MyTest : ITest
{
    public void DoSomething()
    {
        // do something
    }
}
Loge answered 25/1, 2014 at 0:16 Comment(0)
I
2

Here is an example:

interface DisplayMsg
{
    void ShowMsg();
}
/// <summary>
/// Interface  implemented by Struct
/// </summary>
struct StructDisplayMsg : DisplayMsg
{

    public void ShowMsg()
    {
        Console.WriteLine("Inside struct Showmsg:");
    }
}
/// <summary>
/// Interface implemented by Class
/// </summary>
class ObjectDisplayMsg:DisplayMsg
{
    public int Integer { get; set; }

    public void ShowMsg()
    {
        Console.WriteLine("Inside Object ShowMsg:{0}", Integer);
    }
    /// <summary>
    /// Implicit operator for boxing value type to object
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static implicit operator ObjectDisplayMsg(int value)
    {
        ObjectDisplayMsg classObject = new ObjectDisplayMsg();
        classObject.Integer = value;
        return classObject;
    }
}

private void CheckConcepts()
{
    StructDisplayMsg localDisplay = new StructDisplayMsg();
    localDisplay.ShowMsg();

    int localInteger = 10;
    /* Boxing of the integer type to  Object */
    ObjectDisplayMsg intobject = (ObjectDisplayMsg)localInteger;
    intobject.ShowMsg();
    Console.ReadKey();
}
Incapacious answered 25/1, 2014 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.