Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: params IList #14830

Closed
jreumuth opened this issue Oct 31, 2016 · 5 comments
Closed

Proposal: params IList #14830

jreumuth opened this issue Oct 31, 2016 · 5 comments

Comments

@jreumuth
Copy link

jreumuth commented Oct 31, 2016

I frequently have to write functions such as the following:

public void Foo(params string[] p) {
	Foo((IList<string>)p);
}

public void Foo(IList<string> p) {
	// ...
}

It would be nice if I could just do:

public void Foo(params IList<string> p) {
	// ...
}

and this would effectively be the same as the first two functions above, i.e. I could pass in an string[], List, or "abc", "def", "123".

This would also make some bugs less likely. For example, if you have:

public void Foo<T>(params T[] p) {
	Foo((IList<T>)p);
}

public void Foo<T>(IList<T> p) {
	// ...
}

And you do this:

List<int> list = new List<int>();
Foo(list);

This will (unexpectedly) call the first overload with params T[], making the List the first element of an array of . If the first overload didn't exist, then Foo(list) would call the second overload as expected.

If you could just define:

public void Foo<T>(params IList<T> p) {
	// ...
}

Then Foo(list) would do the expected thing.

@dsaf
Copy link

dsaf commented Oct 31, 2016

#36

@gafter
Copy link
Member

gafter commented Nov 1, 2016

Yes, if we do #36 we would make it work for any type for which there is an implicit reference conversion from the array type. Thus this would work as a side-effect of #36.

@DavidArno
Copy link

@dsaf, @gafter,

My immediate reaction to this suggestion was "why IList<T>, why not IEnumerable<T>?". Good to see #36 has that covered and that it's something that might really happen.

@jreumuth
Copy link
Author

jreumuth commented Nov 4, 2016

Thanks for the comments. Yes, this is basically a duplicate of #36 which I wasn't aware of.

@DustinCampbell
Copy link
Member

This language design issue has been moved over to dotnet/csharplang#179.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants