Skip to content

Tricky Array

zijianhuang edited this page Apr 7, 2017 · 6 revisions

Javascript and TypeScript basically support only single dimensional array. In order to support multidimensional array, you will have to use array of array (jagged array).

.NET supports both multidimensional array and jagged array. If your Web API use multidimensional arrays in the POST body or the function return or in a complex type, the CodeGen always translate them to types of jagged arrays for TypeScript codes generated, while keeping the original array types for CSharp codes generated.

The Help Page generated gives some misleading info about multidimensional array, as of ASP.NET Web API 2.2.

Obviously the data type in question is not "Collection of Integer", and there's apparently a bug in the Help Page generation. If developers code clients according to the Help Page without doc comment clarifying that, things may turn puzzling.

With the client API generated, developers will see the correct data types in their end.

        /** 
         * GET api/SuperDemo/int2d
         * @return {number[][]} 
         */
        GetInt2D(callback: (data : number[][]) => any){
            this.httpClient.get(encodeURI(this.baseUri + 'api/SuperDemo/int2d'), callback, this.error, this.statusCode);
        }

        /** 
         * GET api/SuperDemo/int2dJagged
         * @return {Array<Array<number>>} 
         */
        GetInt2DJagged(callback: (data : Array<Array<number>>) => any){
            this.httpClient.get(encodeURI(this.baseUri + 'api/SuperDemo/int2dJagged'), callback, this.error, this.statusCode);
        }

In TypeScript, number[][] is the same as Array<Array> semantically and technically.

public async Task<System.Int32[,]> GetInt2DAsync() ...

public async Task<System.Int32[][]> GetInt2DJaggedAsync()...

Remarks: In .NET, a jagged array is faster than a multidimensional array. And Web API data binding handles well the transformation between jagged array and multidimensional array.

Clone this wiki locally