Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are these asterisks when I generate a constructor with 2D arrays as parameters in Visual Studio?

In Visual Studio for Mac, if I wrote something like this:

public class Foo {
    public int[,] Property1 { get; }

}

Put the cursor on the empty line there, and press Command + I to generate a constructor:

enter image description here

And this happens:

public class Foo {
    public int[,] Property1 { get; }
    public Foo(int[*, *] property1)
    {
        Property1 = property1;
    }
}

The constructor is generated, but the type of the parameter's type is int[*, *], which does not compile.

enter image description here

I am thinking that this is Visual Studio prompting me to write something to replace those asterisks, or maybe not? Since this does not happen with single dimensional arrays, and I do not know of any syntax in C# that allows you to put stuff in [] in a place like this.

For a three dimensional array, it generates 3 asterisks:

public Foo(int[*, *, *] property1)
{
    Property1 = property1;
}

What is causing Visual Studio to do this?

My version of Visual Studio is: Visual Studio for Mac Community 7.2.2 (build 11).

I tried to test this on the Windows version of Visual Studio - Visual Studio Community 2015. But I failed to find a button that does this. According to this answer, there is a generate constructor button in "Quick Actions", but I failed to find one. Maybe it's not in the Community version?

like image 441
Sweeper Avatar asked Jan 29 '23 14:01

Sweeper


2 Answers

    int[*, *] property1

This is caused by a Visual Studio bug. The odd-looking syntax matches the appearance of a non-conformant array. An array is conformant, and its syntax supported by C#, when its lower bounds are 0. C# has no syntax available for array types that are not conformant, thus the weirdo [*,*] and the compile errors it generates.

This normally only happens in C# when you interop with a foreign type system that is capable of creating such array types. I only know of COM as a practical example that is capable of emitting type info that can be directly consumed by a C# program. Note the syntax match in this existing question, another one here.

But of course the type of your property is not special. VS is losing its marbles in the Roslyn code integrated in the IDE. The team that works on this does not have a good track record of writing bug-free tested code. I cannot get a repro for this myself, I'm on version 15.5.6 and have avoided updating to 15.6.x due to the large number of bug reports it has generated. Minor odds that it is specific to the Mac edition, the OS is COM-challenged. VS2015 did not yet have this feature, which is why you can't find it, ReSharper is a popular commercial alternative.

Inside VS use Help > Send Feedback > Report a Problem to tell them about the bug. I don't see an existing bug report for this defect, at least no match when searching developercommunity.visualstudio.com for "generate constructor". I don't expect many programmers running into this bug or taking the time to report it. The workaround is simple, just edit the code to int[,] property1.

like image 142
Hans Passant Avatar answered Jan 31 '23 03:01

Hans Passant


I'm not sure why VS for Mac puts those asterisks there, but I think you want to remove them. Perhaps it's a bug in VS. The following code works for me.

public class Foo
{
    public int[,] Property1 { get; }
    public Foo(int[,] property1)
    {
        Property1 = property1;
    }
}

var myArray = new int[,] { { 1, 2 }, { 3, 4 } };
var foo = new Foo(myArray);
like image 21
asherber Avatar answered Jan 31 '23 03:01

asherber