Dot Net Perls
Top 37 C# Example Pages

["+.syywrr.XYF#CCEE(ST~~}T~~P8G56666G56666+CP#CS}T~~}T~~","Flatten array."," A multidimensional array can be flattened. Its dimensions are reduced to one. This transformation yields a single-dimensional array\u2014one that is simpler and faster. Flattened 2D arrays are also ideal for interop with other languages. ","Flattened array index computation","\n\narray[(Y coordinate * width) + X coordinate]\n\n","2D array index computation","\n\narray[Y coordinate, X coordinate]","Intro."," A 2D array is accessed with a Y and then X position. For rectangular arrays (including all 2D arrays and many jagged arrays) you can use a single array. You multiply the first coordinate by the width, and then add the second coordinate. ","Advantages: ","The advantages of using a flat array are improved performance and interoperability with C++ or other languages.","Note: ","To use a jagged array, you must have an array of references to arrays. 2D arrays have significant performance penalties.","2D Arrays ","2d","Jagged Arrays ","jagged-array","Example."," This example creates, assigns to and finally displays a 2D array and its equivalent flattened array. It is contained in a console program with a simple command-line interface. Specify a height of at least 4, and a width of at least 10. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","C# program that uses flattened array","\n\nusing System;\n\nclass Program\n{\n static void Main()\n {\n while (true)\n {\n Console.WriteLine(","\"Enter height: [4+]\"",");\n int height = int.Parse(Console.ReadLine());\n\n Console.WriteLine(","\"Enter width: [10+]\"",");\n int width = int.Parse(Console.ReadLine());","\n\n //\n // A. TWO-DIMENSIONAL ARRAY\n //\n ","int[,]"," twoDimensional = new int[height, width];","\n // Assign cell 1, 6\n ","twoDimensional[1, 6] = 5;","\n // Assign cell 3, 9\n ","twoDimensional[3, 9] = 9;","\n // Assign cell at 2, 3\n ","twoDimensional[2, 3] = 1;","\n\n // Display\n ","for (int i = 0; i < height; i++)\n {\n for (int a = 0; a < width; a++)\n {\n Console.Write(twoDimensional[i, a]);\n }\n Console.WriteLine();\n }\n Console.WriteLine();","\n\n //\n // B. FLATTENED ARRAY\n //\n ","int[]"," oneDimensional = new int[width * height];","\n // Assign cell 1, 6\n ","oneDimensional[1 * width + 6] = 5;","\n // Assign cell 3, 9\n ","oneDimensional[3 * width + 9] = 9;","\n // Assign cell at 2, 3\n ","oneDimensional[2 * width + 3] = 1;","\n\n // Display\n ","for (int i = 0; i < height; i++)\n {\n for (int a = 0; a < width; a++)\n {\n Console.Write(oneDimensional[i * width + a]);\n }\n Console.WriteLine();\n }\n }\n }\n}","The example"," has some complexity. Part A shows the 2D array with traditional syntax. Part B shows the flattened 1D array. Notice how we use multiplication always when accessing the oneDimensional flat array. ","Next: ","We see some output from the program that demonstrates how the multiplication results in correct output.","Possible output","\n\nEnter height: [4+]\n4\nEnter width: [10+]\n10\n0000000000\n0000005000\n0001000000\n0000000009\n\n0000000000\n0000005000\n0001000000\n0000000009\n\nEnter height: [4+]\n5\nEnter width: [10+]\n15\n000000000000000\n000000500000000\n000100000000000\n000000000900000\n000000000000000\n\n000000000000000\n000000500000000\n000100000000000\n000000000900000\n000000000000000\n\nEnter height: [4+]\n6\nEnter width: [10+]\n12\n000000000000\n000000500000\n000100000000\n000000000900\n000000000000\n000000000000\n\n000000000000\n000000500000\n000100000000\n000000000900\n000000000000\n000000000000","Summary."," We can create, assign values to, and display flattened arrays. This is useful for performance, memory reduction, and interoperability with other systems. In every application I have applied this technique, performance has improved. ","And: ","Flattened arrays are ideal for hashtable bucket implementations as well as certain tree structures.","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANYAAAA3CAMAAABHJUQ7AAAAMFBMVEX////7/f7y/f+38v//6en/1dX/tbX/xcXl+v/U9//G9f//qKiq8P+U7P+e7v//lJQSDb6dAAAEaklEQVR4Xt2Z27KiMBBF050bN/X//3aQiBvZoeMZHoYz+8myKYtFeuWCDomfcY30n3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