twilio-csharp icon indicating copy to clipboard operation
twilio-csharp copied to clipboard

Use `IList` or `IEnumerable` instead of `List` in method parameters

Open Swimburger opened this issue 2 years ago • 3 comments

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 :)

Swimburger avatar Apr 27 '22 18:04 Swimburger

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.

JenniferMah avatar Apr 28 '22 21:04 JenniferMah

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 :)

Swimburger avatar Apr 29 '22 17:04 Swimburger

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

Swimburger avatar Aug 29 '22 17:08 Swimburger