Constant array initialization in attribute is inconsistent #699
Replies: 11 comments
-
var myArray = new int[1];
myArray[1] = 0;
// EQUAL TO
var myArray = new int[] { 0 }; Similar reason why you can't use classes due to the constructor. The only allowed types are;
|
Beta Was this translation helpful? Give feedback.
-
@BraedonWooding I think you have this backwards... Also you've said "The only allowed types are... one-dimensional arrays of any of the above types" where "the above types" includes "int".
|
Beta Was this translation helpful? Give feedback.
-
Yes but new int[] { 0 } isn't a constant value... which is what I was meaning. That int[] is but the initializer makes it non-constant. |
Beta Was this translation helpful? Give feedback.
-
Like I said, I think you have it backwards.
It's |
Beta Was this translation helpful? Give feedback.
-
Personally I prefer that the C# compiler forces you to supply the constant values for each element in the array, even if those values would be the default values. As an imposed limitation there must have been an explicit design choice that went into the requirement of supplying those values. |
Beta Was this translation helpful? Give feedback.
-
Ahhhh I see don't mind me being stupid a little :P. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour obviously for |
Beta Was this translation helpful? Give feedback.
-
You're writing that validation in attributes? |
Beta Was this translation helpful? Give feedback.
-
@HaloFour for example xunit uses attributes to pass test data to test methods. I've been writing tests for crypto and system.drawing over in corefx. These libraries use the pattern of passing an array as a parameter to a method or value to a property and validating the length, so I have encountered this design decision many many times. |
Beta Was this translation helpful? Give feedback.
-
I see. I don't particularly care either way but I'm loathe to push for language changes specifically to suit the behaviors of a specific third-party library/framework. Data-driven tests in xunit appear to have a fairly pluggable method of providing test data so you're not strictly limited to trying to stuff giant byte arrays directly into an attribute (especially where I'm sure those arrays are likely to contain something other than all zeroes anyway). For example (and I'm not necessarily suggesting this as a solution to your problem), you could extend |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply and suggestion! That's why I was hesitant to bring the xunit part up in the first place, I was afraid it would limit the discussion of what I see as an inconsistency in the language to specific use cases. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The following code fails to compile with
Replacing
new int[1]
withnew int[] { 0 }
, which is identical, works.I suggest that both should compile, rather than just
new int[] { 0 }
Beta Was this translation helpful? Give feedback.
All reactions