Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous type as method parameter

My first try to use anonymous types(test):

    private void button4_Click(object sender, EventArgs e)
    {
        test(new { a = "asd" });
    }

    private void test(string a)
    {
    }

I get an error "cannot convert from 'AnonymousType#1' to 'string' "

Also I'd like to know how to pass an anonymous type if the parameter is a string[]

    private void test(string[] a)
    {
    }

Thanks in advance!

like image 368
Ivan Prodanov Avatar asked Apr 22 '26 18:04

Ivan Prodanov


1 Answers

Something about your design is faulty. If your test function accepts only strings, then you can never pass an anonymous type to it, just as you can't pass an int, object, or any other non-string type.

like image 56
John Fisher Avatar answered Apr 24 '26 08:04

John Fisher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!