twilio-csharp
twilio-csharp copied to clipboard
Use `IList` or `IEnumerable` instead of `List` in method parameters
There may be a reason List<T>
is used instead of IList<T>
or IEnumerable<T>
, but I'd like to be able to pass in other types of collection classes, specifically T[]
.
For example, here's a snippet that generates TwiML that will gather some input:
var voiceResponse = new VoiceResponse()
.Gather(
input: new List<Gather.InputEnum> {Gather.InputEnum.Dtmf}
);
It works fine, but if the input
parameter would accept IList<Gather.InputEnum>
or IEnumerable<Gather.InputEnum>
, we could pass in an array instead:
var voiceResponse = new VoiceResponse()
.Gather(
input: new[]{Gather.InputEnum.Dtmf}
);
This is super minor, but this would make the APIs a little more flexible and allow developers to write shorter/less verbose code IMO.
This isn't the only location List<T>
is specified, so I'd like to see it changed, if possible, wherever :)
This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.
Here's some more ideas:
var voiceResponse = new VoiceResponse()
.Gather(
input: InputEnum.Dtmf + InputEnum.Speech
);
var voiceResponse = new VoiceResponse()
.Gather(
input: InputEnum.Dtmf & InputEnum.Speech
);
var voiceResponse = new VoiceResponse()
.Gather(
input: InputEnum.Dtmf.ToArray()
);
var voiceResponse = new VoiceResponse()
.Gather(
input: InputEnum.Dtmf.ToList()
);
This could be accomplished by extending the enum classes like this:
public sealed class InputEnum : StringEnum
{
...
public static InputEnum[] operator +(InputEnum a, InputEnum b) => new[] {a, b};
public static InputEnum[] operator &(InputEnum a, InputEnum b) => new[] {a, b};
public List<InputEnum> ToList() => new List<InputEnum>{this};
public InputEnum[] ToArray() => new []{this};
...
}
Personally, I hope someone can come up with better ideas :)
FYI, for VB .NET, you could do this
Dim gather = new Gather(
input := {Voice.Gather.InputEnum.Speech}
)
Instead of this
Dim gather = new Gather(
input := New List(Of Gather.InputEnum) From {Voice.Gather.InputEnum.Speech}
)
If we'd allow IList<Gather.InputEnum>
or IEnumerable<Gather.InputEnum>
instead of List<Gather.InputEnum>
.