• 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 /
avatar image
Question by Der-Die-Das · May 07, 2018 at 11:40 AM · c#inheritancegenericsgeneric

C# Generic Type Inheritance

Hey everyone. I have some problems with the inheritance of Generic classes. In the example below there is a exception thrown when I try to initialize the List.. It seems like it can't cast from MiningBuilding (Building) to Building. I've heard that t$$anonymous$$s should be possible in Java. Does anyone see a solution for t$$anonymous$$s?

Not tested Code:

 abstract class BuildingType : ScriptableObject{ }
 class MiningBuildingType : BuildingType { }
 abstract class Building<T> where T : BuildingType 
 {
   public T Type;
 }
 class MiningBuilding : Building<MiningBuildingType> 
 { 
   public MiningBuilding(T type)
   {
     Type = type;
   }
 }
 
 ..
 public MiningBuildingType type;
 List<Building<BuildingType>> buildings = new List<Building<BuildingType>>(){ new MiningBuilding(type) };
 





Comment

People who like this

0 Show 11
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 misher · May 07, 2018 at 12:21 PM 1
Share

Very strange pattern, in unity you would like to use composition whenever you can. Looking at your snippet it can be syntax error, there are misssing parentecices in list declaration...

 List<Building<BuildingType>> buildings = new List<Building<BuildingType>>(){ new MiningBuilding() };





avatar image Der-Die-Das misher · May 07, 2018 at 12:27 PM 0
Share

Yeah, that's not actual code. I just wrote that to show the problem. I'm using composition. But I want the Field in MiningBuilding to be of type MiningBuildingType. That field is decalred in its base class (Building) of type BuildingType.

Do you get what im trying to say?

avatar image misher Der-Die-Das · May 07, 2018 at 12:35 PM 0
Share

I just don't get what do you want to achieve b saying "MiningBuiulding of MiningBuildingType". It sounds very strange. Every class itself IS a type, at least this is how I am reasoning about them. Talking about Unity, you also should take into consideration MonoBehaviours (as a front-end for your data structure) then maybe wrap your underlying data structure with scriptable objects containers.

Show more comments
avatar image Der-Die-Das misher · May 07, 2018 at 12:32 PM 0
Share

I've updated the question a bit.

avatar image misher Der-Die-Das · May 07, 2018 at 12:36 PM 0
Share
 List<Building<BuildingType>> buildings = new List<Building<BuildingType>>(){ new MiningBuilding() };


I mentioned missing parenteces befor, its just got eaten by comment editor :(

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Firas4d · May 07, 2018 at 01:48 PM

Hi, To simplify your example allow me to take the list and the generic definition out of the equation. What you are trying to do is casting a class to another incompatible class implicitly without providing a mechanism telling the compiler how to do the casting.

 Class A {}
 Class B {}
 ...
 A obj = new B();

The code above will generate the same error you got because A and B are 2 separate types.

To solve t$$anonymous$$s error you have 2 options, either you implement a user defined type conversion like t$$anonymous$$s

 public class B 
 {
     public static implicit operator A(B obj)
     {
         return new A();
     }
 }

or you just simply make B inherits from A.

Now lets get back to the generic class definition, take a look at the following example

 //Behaviour is a Unity class where MonoBehaviour inherits from
 Class A<T> where T : Behaviour {}
 A<Behaviour> aBehaviour = new A<MonoBehaviour>();

t$$anonymous$$s will not compile successfully because the generic class A(Behaviour) is completely different than A(MonoBehaviour) even though MonoBehaviour is related to Behaviour and both types are supported by class A.

Put all that aside, IMO I see no need to have a generic class in your case, it just makes your code unnecessarily complicated. If you just do the following all will be fine.

 Class Building {}
 Class MiningBuilding : Building {}
Comment
Harinezumi

People who like this

1 Show 0 · 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

Answer by misher · May 07, 2018 at 12:55 PM

You can't have a constructor like t$$anonymous$$s:

 class MiningBuilding : Building<MiningBuildingType>
 {
     public MiningBuilding(T type)
     {
         Type = type;
     }
 }

MiningBuilding could only have field "Type" of type MiningBuildingType (type of type lol)

 class MiningBuilding : Building<MiningBuildingType>
 {
     public MiningBuilding()
     {
         Type = new MiningBuildingType();
     }
 } 

Now declaring a List like t$$anonymous$$s:

 List<Building<BuildingType>>

won't let you to create it, because you can't create any instance of abstract class (building)

Adding intermediate building class can enable you to create generic list of buildings:

 abstract class BuildingBase<T> where T : BuildingTypeBase
 {
     public T Type;
 }
 
 abstract class BuildingTypeBase
 {
 
 }
 class MiningBuildingType : BuildingTypeBase { }
 
 class Building : BuildingBase<BuildingTypeBase>
 {
 
 }
 
 class MiningBuilding : Building
 {
     public MiningBuilding()
     {
         Type = new MiningBuildingType();
     }
 }
 
 public class Test
 {
     List<Building> buildings = new List<Building>();
     void myCode()
     {
         var b = new MiningBuilding();
         buildings.Add(b);
     }
 
 }
Comment

People who like this

0 Show 3 · 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 Der-Die-Das · May 07, 2018 at 01:22 PM 0
Share

Yeah, sure. But like that I always have to cast "b" to MiningBuilding to use its members. I was looking for a solution where I don't need to do that.

avatar image misher Der-Die-Das · May 07, 2018 at 01:29 PM 1
Share

Just rethink everything and use components, ech single component can have own inheritance tree, whenever you wnat to find all buildings with some special buildingtype component, you will always be able to do it. What you need is to find out how to store persistent data and how to build your front-end with monobehaviours and components to consume these data. There can be also third layer, transferrable data format (in case you want to have some networking)

avatar image Der-Die-Das misher · May 07, 2018 at 01:42 PM 1
Share

Thanks for your effort but that really doesn't help me. I've opened a Thread on StackOverflow and hope I'll find an answer on there. https://stackoverflow.com/questions/50215653/c-sharp-generic-class-inheritance-confusion

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

496 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 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 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 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 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 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 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 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

An OS design issue: File types associated with their appropriate programs 1 Answer

C# Inheriting from MonoBehaviour but still be able to instantiate with new()? How should I do this? 2 Answers

Multiple Cars not working 1 Answer

C# Generics not constraining correctly? 1 Answer

Distribute terrain in zones 3 Answers


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