Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by PlayCreatively · Jan 15, 2017 at 01:17 PM · functionreferenceinheritanceinterfacekeyword

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();
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 15, 2017 at 03:00 PM 2
Share

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.

avatar image PlayCreatively Bunny83 · Jan 15, 2017 at 04:46 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PlayCreatively · Jan 15, 2017 at 04:31 PM 0
Share

Great! Could you point out what about this design is bad and how I could make it better?

avatar image Mikilo · Jan 15, 2017 at 04:44 PM 0
Share

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.

avatar image PlayCreatively Mikilo · Jan 15, 2017 at 04:59 PM 0
Share

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?

avatar image Bunny83 PlayCreatively · Jan 16, 2017 at 02:14 AM 1
Share

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.

Show more comments
avatar image
1

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;
  }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

62 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Serialize Lists with multiple inherited classes or interfaces ? 1 Answer

Using the 'is' operator versus an abstract enum in identifying a child class. 0 Answers

Inheritance Question 1 Answer

Can't instantiate new class C# 0 Answers

Help me to get rid of repetitive code 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges