Are interfaces derived from a base class?
I have an interface called IPickUpable
which contains the function void UseOn(ref interface item);
, which means that I want to run the function UseOn()
to send in a reference of type interface i.e. IPickUpable
. This doesn't work, probably because the word interface
is a keyword and not really a class (I think). Are there any workarounds to this problem?
(Here is all of my code)
public interface IPickUpable
{
void Highlight();
void PickUp();
bool Use(); //Returns true if resulting in self-destruction
void UseOn(ref interface item);
void Drop();
}
The others have already answered your question. However i'm just wondering why you used "ref" here. Are you sure you know what "ref" is good for? I recently posted a detailed explanation.
An interface type is always a reference type, even when the object is a struct. The struct will be boxed automatically when you cast to an interface type just like it would when you pass it to an "object" variable / parameter.
Oh, didn't know that. You're right, I'm trying to get a reference to the object through its interface so I thought I needed to use ref to make sure it won't make a copy of the object ins$$anonymous$$d of giving me a reference. Thanks for the heads-up ;)
Answer by Mikilo · Jan 15, 2017 at 01:51 PM
Hello,
As you mentioned, interface is a keyword and not a type.
The first thing I want to point out, is what you are trying to do is bad design.
But to help you, you can replace interface with either IPIckUpable if you only want to work on IPickUpable. Or replace it with object, for any type.
System.Object is the most basic type in C#. It can be either a class, struct, array, primitive types or else (Interface included).
Great! Could you point out what about this design is bad and how I could make it better?
Bunny83 said it in the comment.
$$anonymous$$eyword interface implicitly implies that the object your are working on is a ref-type.
Adding ref in your argument won't add you anything except bad lisibility.
Do you understand? If you want more explanation just ask.
How does the keyword interface implicitly imply that it's a ref-type? Do you mean that the keyword interface implies that in the documentations?
Just to make that clear: An interface is also a type. An interface is just "a signature contract". An interface just dictates which methods, events, properties or indexer. An interface can not contain any data or even code. If a class or struct implements an interface that class / struct has to implement everything that is specified in the interface.
Yes, structs can also implement interfaces. However an interface type is always a reference type like a class. So whenever you treat a struct as interface the struct is boxed on the heap.
If you have no idea what boxing and unboxing is, see this $$anonymous$$SDN page. $$anonymous$$ake sure you read through it carefully.
A quick example of a struct that implements an interface:
public interface I$$anonymous$$yInterface
{
public int GetValue();
}
public struct $$anonymous$$yStruct :
{
public int val;
public int GetValue()
{
return val;
}
}
// somewhere else
void Example()
{
$$anonymous$$yStruct struct = new $$anonymous$$yStruct();
struct.val = 5;
// Here the local struct is boxed on the heap and the reference is stored in "obj"
I$$anonymous$$yInterface obj = struct;
struct.val = 42;
Debug.Log("value: " + obj.GetValue()); // prints "5" and NOT 42
// [...]
}
You should avoid using interfaces with structs unless you completely understand what's happening. Structs are immutable constructs by definition. If you want to pass a struct around and want the method to change something you have a design problem. That struct should be a class then.
Answer by IgorAherne · Jan 15, 2017 at 01:54 PM
Supply the most ambiguous type of interface which accommodates for any possible interface-child that could be plugged-in into your UseOn function. This means that IPickUpable must (eventually) inherit from MyBaseInterface.
void useOn(ref MyBaseInterface item){
}
be sure to read this question as well http://answers.unity3d.com/questions/1292671/how-to-make-method-that-gets-multiple-types.html
In case if link is broken here is what it said:
Machine myMachine; //came as argument in function
Furnace machineAsFurnace = myMachine as Furnace;
if(machineAsFurnace != null){
//machine as furnace is a furnace! success.
//do something with your furnace:
machineAsFurnace.Smelt(myIngots);
return;
}
Sawmill machineAsSawmill = myMachine as Machine;
if(machineAsSawmill != null){
//do something with your sawmill
return;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612104843im_/https://answers.unity.com/themes/thub/images/avi.jpg)