Using Target-Typed New with the params keyword #4614
Answered
by
333fred
brandon-irl
asked this question in
Q&A
-
Can someone tell me why I can't do this? static void Main(string[] args)
{
DoSomething(new DateTime()); // This is fine
DoSomething(new ()); // ERROR: The type 'DateTime[]' may not be used as the target type of new()
}
private static void DoSomething(params DateTime[] dates)
{
// TODO
} |
Beta Was this translation helpful? Give feedback.
Answered by
333fred
Apr 1, 2021
Replies: 1 comment 5 replies
-
Let me answer your question with a question: were you expecting that to give you a new array, or were you expecting that to give you a new |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
brandon-irl
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let me answer your question with a question: were you expecting that to give you a new array, or were you expecting that to give you a new
DateTime
? Because the compiler inferred the former (and has no choice but to infer the former), and to create a new array you need an array creation expression, not a constructor.