1

Take a look at at the code below.

void Foo1<T>() where T : struct, IBar1, IBar2, IBar3 {}
void Foo2<T>() where T : struct, IBar1, IBar2, IBar3 {}
void Foo3<T>() where T : struct, IBar1, IBar2, IBar3 {}

How can I alias the constraints on the right side of each declaration so that those declarations can become shorter and more readable?

Conceptually, what I'm after should look similar to this (not valid C# code):

MyConstraint<T> = T : struct, IBar1, IBar2, IBar3;
void Foo1<T>() where T : MyConstraint {}
void Foo2<T>() where T : MyConstraint {}
void Foo3<T>() where T : MyConstraint {}

That T : struct is the most crucial part for my use case.

If there is no such grammar in C#, what alternatives do you suggest to reduce the clutter here?

diegoperini
  • 1,796
  • 21
  • 37
  • 2
    There is no such syntax in C# (at least, for now). But you can create a type which match the constraint `where T : struct, IBar1, IBar2, IBar3` and use it as generic constraint. There is an open issue [#1239](https://github.com/dotnet/csharplang/issues/1239) for generic type aliases – Pavel Anikhouski Mar 15 '20 at 17:38
  • Does this answer your question? [Using a 'using alias = class' with generic types?](https://stackoverflow.com/questions/4936941/using-a-using-alias-class-with-generic-types) – Pavel Anikhouski Mar 15 '20 at 17:45
  • @PavelAnikhouski Unfortunately it doesn't. Maybe it is not possible yet. – diegoperini Mar 30 '20 at 07:47
  • Yes, the linked GitHub proposal is still open – Pavel Anikhouski Mar 30 '20 at 07:50

0 Answers0