Some functions only accept arrays as arguments but you want to assign a single object to them. For example to assign a primary key column for a DataTable
I do this:
DataColumn[] time = new DataColumn[1];
time[0] = timeslots.Columns["time"];
timeslots.PrimaryKey = time;
This seems cumbersome, so basically I only need to convert a DataColumn
to a DataColumn[1]
array. Is there any easier way to do that ?
You can write it using the array initializer syntax:
timeslots.PrimaryKey = new[] { timeslots.Columns["time"] }
This uses type inference to infer the type of the array and creates an array of whatever type timeslots.Columns["time"] returns.
If you would prefer the array to be a different type (e.g. a supertype) you can make that explicit too
timeslots.PrimaryKey = new DataColumn[] { timeslots.Columns["time"] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With