There is a class:

public class Resource<T> : IGame where T : Component { 

And for example, we use it as a type in some kind of interface:

 public interface IBuildResource { Resource<Component> GetPath(List<Resource<Component>> RefList); } 

Is it acceptable practice to write <Resource<Component>> instead of <Resource<T>> and, accordingly, make the interface itself generic?

Or, for example, in the class functions implementing this interface, write:

 class BuildComponent<T> : BaseCls, IBuildResource { public Resource<Component> GetPath(List<Resource<Component>> resList) { Resource<Component> resReference = null; . . . return resReference; } 
  • 3
    do whatever the compiler can understand. if it doesn’t fall with an error it means acceptable :) - PashaPash
  • one
    does the author have in mind acceptable use practices? What are eligibility criteria? - Anatol

1 answer 1

The last example with BuildComponent<T> most likely incorrect, because it looks like this:

 List<int> list; list.Remove("item"); list.Find<String>() 

Usually such methods still have some connection with T

 public Resource<T> GetPath(List<Resource<T>> resList) where T : Component 

Honestly, I would think hard over the embedded Generics. They are just awkward to read and write. Instead of List<Resource<Component>> I would prefer to see

 class ComponentList : ResourceList<Component> { } class ResourceList<T> : List<Resource<T>> { } 

It does not require much time and effort, from the extra code - only the need to declare the designers, but the code will be much cleaner and more pleasant to the eye. Extremely subjective opinion. Some use aliases instead, but it seems to me that they will confuse rather than help. We don't have typed macros yet, unlike Nemerle .

  • I also don’t like this practice, but unfortunately this is a code that needs to be deflated and everything can be changed into a more readable version. - Alex
  • And why, if not a secret? In the above proposed variant, you can pass a ComponentList to a method that accepts List<Resource<Component>> due to inheritance, and bulk edits are easy to implement thanks to ReSharper 's refactoring. - Lunar Whisper
  • Because in large projects with a large number of people it is impossible to take something and change something, you can only change strictly specified places)) - Alex
  • I will go to my small project for 200 developers. : D - Lunar Whisper