0

So recently I have stumbled upon a youtube video(I'm not sure i can link it) about efficiency in c#, mainly writing efficient code for a unity engine game. So at some point in the video he showcases a struct that implements an interface with arbitrary number of generic parameters. Here is a picture to illustrate what he did:

enter image description here

The interface I am trying to replicate is the IJobsProcess. So what I got from what he said in the video is that the interface can take an arbitrary number of generic parameters. In conclusion how can i create an interface like that in c#? And please correct me if I said anything wrong here.

  • 5
    I don't think you understood correctly, there is no way to have an arbitrary amount of generic parameters. The interface in the picture has 2 generic type parameters – Titian Cernicova-Dragomir Feb 04 '18 at 19:19
  • Not sure if this is what you're looking for, but you can use a [`params object[] args`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params) argument in C# to allow `0-many` object arguments passed to a method. – Rufus L Feb 04 '18 at 19:23
  • Could you paste link to video, just curious.. – Risto M Feb 04 '18 at 19:28
  • Here is the link to the video at 56:11"https://youtu.be/tGmnZdY5Y-E?t=56m11s" around this time he showcases the above implementation – ionut george soran Feb 04 '18 at 19:33

1 Answers1

3

You can write and implement interface with any amount of generic parameters. But you cannot write interfaces with arbitrary amount of generic parameters.

Here is example for you:

interface IService<T1, T2>
{
    T1 GetValue();
    T2 GetAnotherValue();
}
class MyClass : IService<string, int>
{
    public int GetAnotherValue()
    {
        return 42;
    }

    public string GetValue()
    {
        return "Truth";
    }
}

As you see above, number of generic parameters can be more than 1, but that number is fixed in compile-time.

Same question is asked also here and here. In those questions there are mentioned few alternatives, where arbitrary amount of Type-parameters are implemented with other common known C#-features: with params keyword or passing array of types as parameter.

Risto M
  • 2,919
  • 1
  • 14
  • 27
  • I see your point, nonetheless if i may here is a link to the video "https://www.youtube.com/watch?v=tGmnZdY5Y-E" at 55:46 he implements the above mentioned code, could i get some insight on what he is doing there, from your point of view ? I got a little confused there and still am. – ionut george soran Feb 04 '18 at 19:43
  • In the video guy showed just normal good-old-generic type (with 2 generic arguments). Basically it was same case as my example. In my example there is IService interface which takes T1 and T2, and MyClass implements IService with T1=string and T2=int. In video, implementation class was GravityMinionJob, which implemented IJobProcessComponentData with generic types T1=MinionVelocity and T2=RotationSpeed. I think you can now mark this question answered. – Risto M Feb 04 '18 at 19:55
  • So bottom line is, he just has multiple implementations of the generic interface like say MyInterface or MyInterface or Myinterface etc. i presume? – ionut george soran Feb 04 '18 at 20:03
  • Yes, he has multiple separate interfaces which has different amount of generic parameters. – Risto M Feb 04 '18 at 20:06
  • Thank you for clearing this up for me! Have a nice day mister! – ionut george soran Feb 04 '18 at 20:08
  • Thanks! Happy hacking times to you! – Risto M Feb 04 '18 at 20:16
  • 1
    A good example is the Action Func classes. I think they have like 9 different versions from no parameter to 8 parameters.http://referencesource.microsoft.com/#mscorlib/system/action.cs,486d58da4553e12d so no param style case for generic. That's what makes them strongly typed. It is explicitely open but at compile time, the type is clearly defined. – Everts Feb 04 '18 at 22:13