While writing unit tests, I came across a problem: NUnit [TestCaseAttribute]with the following constructor overloads:
public TestCaseAttribute(params object arg)
public TestCaseAttribute(params object args[])
accepts an array of integers and a list of arguments, including an array of strings, but does not accept an array of strings:
[TestCaseAttribute(new[] { 1, 2, 3 })]
[TestCaseAttribute("Other string", new[] { "1", "2", "3" })]
[TestCaseAttribute(new[] { "1", "2", "3" })]
This surprised me, so I checked the behavior:
private static void PrintTypes(params object[] objects)
{
Console.WriteLine("Array type is " + objects.GetType());
Console.WriteLine("Object count is " + objects.Length);
Console.WriteLine("Object type is " + objects[0].GetType());
}
public static void Main()
{
Console.WriteLine("Array of ints: ");
PrintTypes(new[] { 1, 2, 3 });
Console.WriteLine();
Console.WriteLine("Array of strings: ");
PrintTypes(new[] { "1", "2", "3" });
}
The result is somewhat confusing to me - it seems that the integer array is considered as one object, but the array of strings is expanded:
Array of ints:
Array type is System.Object[]
Object count is 1
Object type is System.Int32[]
Array of strings:
Array type is System.String[]
Object count is 3
Object type is System.String
And if we add the following method:
private static void PrintTypes(object obj)
{
Console.WriteLine("In object method");
Console.WriteLine("Object type is " + obj.GetType());
}
the compiler prefers it for an int array, but not for an array of strings:
Array of ints:
In object method
Object type is System.Int32[]
Array of strings:
In array method
Array type is System.String[]
Object count is 3
Object type is System.String
? , - , , , , , .