Convert an object to a single item array of object (C#)
Drawing on the answers above, I created this extension method which is very helpful and saves me a lot of typing.
/// <summary>
/// Convert the provided object into an array
/// with the object as its single item.
/// </summary>
/// <typeparam name="T">The type of the object that will
/// be provided and contained in the returned array.</typeparam>
/// <param name="withSingleItem">The item which will be
/// contained in the return array as its single item.</param>
/// <returns>An array with <paramref name="withSingleItem"/>
/// as its single item.</returns>
public static T[] ToArray<T>(this T withSingleItem) => new[] { withSingleItem };
timeslots.PrimaryKey = new DataColumn[] { timeslots.Columns["time"] };
You can also write in one line with array initializer:
timeslots.PrimaryKey = new DataColumn[] { timeslots.Columns["time"] };
Check this out: All possible C# array initialization syntaxes
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"] }