independent_strudel
independent_strudel

Reputation: 1

C# - Different types as method parameter / Creating arrays without using the new keyword

I am quite new to C#, coming from C++. I am writing a testing framework in C#, and I want writing the test cases to be as easy as possible.

I have two questions for which I was not able to find a conclusive solution. Therefore I am asking for a little bit of help.

Question #1

Is it possible in C# to pass different types as parameters for a function?

The way I want to call the function is as follows:

[Test]
    public void TestCase1() => RunTestCase("test_data.csv",
        // from_m, to_m, parameter_to_test, operator,          expected_values
        (  1000,   1e6,  "vehicle_speed",   Operator.Greater,  50.0           // <-- double), 
        (  0,      1e6,  "vehicle_acc",     Operator.MinMax,   {500.0, 700.0} // <-- double[]),
        (  0,      70,   "vehicle_gear",    Operator.ValueSet, {1, 2, 3, 4}   // <-- int []),
        (  0,      10,   "vehicle_gear",    Operator.Gear,     1              // <-- int)
        (...),
    );

As you can see, the expected_values can either be double, double[], int or int[] How can I write the signature of the RunTestCase function to allow writing the test cases like this?

At the moment the RunTestCase function signature is as follows:

public void RunTestCase(string test_data_file, 
                        params (double start,
                                double end, 
                                string property, 
                                Operator op, 
                                /* what to use instead of double */ double values)[] expected)

My question is what type can values have, in order to achieve this?

Can a template be used, similar to C++?

Question #2

Is creating the arrays like I did in the example even possible? What I want to achieve is avoiding having to write new int[] {1, 2, 3} and just write {1, 2, 3}. As I said, I'm mostly interested in ease of writing.

Thanks!

Upvotes: 0

Views: 45

Answers (0)

Related Questions