Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Interface is value type or reference type?

please help
i am totally confused.


thanq.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Oct-11 12:28pm    
Looking simple, this is a very provocative question -- thank you very much, get my 5.
To understand why it's so non-trivial, please see my answer.
--SA

I would add to the basically correct answer by Collin and Mehdi:

Another question is: we declare a variable of interface type, is it of value type reference type?
Interesting question, really. In certain sense, it is always of reference type, because this is a reference to some object. But! For a variable itself only its compile-time type is known, call it "reference to something" if you well. When this variable is assigned to some object, the object could be either reference (in case if implementing type is a class) or value type (in case if implementing type is struct). In case of struct, however, the object is always boxed in a reference type as it appears through the variable of interface type.

A really delicate issue, in a way.

—SA
 
Share this answer
 
v2
Comments
Mehdi Gholam 19-Oct-11 12:36pm    
I was right afterall! 5!
Sergey Alexandrovich Kryukov 19-Oct-11 12:50pm    
Thank you, Mehdi.
Yes, on the after-math, but only because you did not know or did not remember that struct can also implement interfaces. This fact is not well realized by people, and the implication is not trivial; this is actually another kind of polymorphism not based on late binding in the sense of virtual functions (which are not available in struct). Some people explained it to me an year ago or so -- I did not realize it myself. Now, how this is represented via interface reference is not quite trivial.
--SA
An interface is neither but the implementing type can be either

e.g.

C#
public interface ITest
{
    void DoSomething();
}

public class TestClass : ITest
{
    public void DoSomething()
    {
        
    }
}

public struct TestStruct : ITest
{
    public void DoSomething()
    {
        
    }
}


see MSDN[^] for more details
 
Share this answer
 
Comments
Mehdi Gholam 19-Oct-11 11:45am    
my 5!
Sergey Alexandrovich Kryukov 19-Oct-11 12:29pm    
I voted 4, because this is not so easy. Please see my solution and my comment to the answer by Collin.
--SA
Uday P.Singh 19-Oct-11 13:04pm    
Agree my 5!
RaviRanjanKr 20-Oct-11 3:29am    
My 5+
Neither. Interface is a contract.

It can be used on either ref or value types.

For example,

C#
public struct CustomIntCollectionValueType : ICollection<int>
{
  //Implementation here
}
</int>


or

C#
public class CustomIntCollectionRefType : ICollection<int>
{
  //Implementation
}
</int>


In this case I am using a system provided interface. One can create their own interface and implement it upon a value type (struct) or ref type (class).
 
Share this answer
 
v3
Comments
Mehdi Gholam 19-Oct-11 11:46am    
my 5!
[no name] 19-Oct-11 11:53am    
Thank you :)
Sergey Alexandrovich Kryukov 19-Oct-11 12:19pm    
Collin, I fixed you pretty nasty spelling mistake in 3 placed; please learn this word.
--SA
[no name] 19-Oct-11 13:17pm    
Thank you. I am a horrible speller, and unfortunately CP offers no help (spell check). Conent was good though :-)
Sergey Alexandrovich Kryukov 19-Oct-11 12:27pm    
About your answer, I don't know what to say... This is not so simple. In some sense, interface as a variable of such type is always a reference type, as struct appears in a boxed way. This is such a delicate issue -- please see my solution.
Well... I'll vote 4 I guess...
--SA
An interface is an interface, what the underlying object who implemented that interface can be only a reference type as it has to be a class not string, int, ...

EDIT -> I stand corrected, structs can implement interfaces too.
 
Share this answer
 
v2
Comments
[no name] 19-Oct-11 11:38am    
Are you sure about that? I am pretty sure a struct can impliment an interface...
Mehdi Gholam 19-Oct-11 11:43am    
Good point, I never used interfaces with structs.
[no name] 19-Oct-11 11:53am    
Yeah me neither... But then again I never use structs. I will admit, I had to check :-)
Reiss 19-Oct-11 11:39am    
Struct's can implement interfaces too
Mehdi Gholam 19-Oct-11 11:45am    
Thanks, updated the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900