选择数组维度的高效代码实现咨询(附现有代码片段)
Optimizing Your 3D Array Dimension Selection Code
Hey there! Let's clean up that repetitive dimension selection code to make it more efficient and maintainable. Your original snippet has a lot of duplicated logic (like reinitializing the array and calling To3D in every branch) that we can eliminate with a cleaner approach.
First, Let's Spot the Pain Points
- Redundant Code: You're initializing
to3dimentwice per branch and callingTo3Drepeatedly—this is unnecessary and adds clutter. - Scattered Values: Dimension values are hardcoded across multiple conditionals, making updates a hassle (you'd have to change them in 3 places if, say, the 4x8x8 dimension needs tweaking).
- Incomplete Logic: Your last branch cuts off, but we'll account for that in the optimized versions.
Optimized Approach 1: Use a Dictionary for Dimension Mapping
This approach centralizes all your dimension mappings, so you can update values in one place and avoid repetitive setup:
private int[, ,] dim(int[] m) { // Start with default dimensions (adjust these if you have a fallback case) int ww = 0, ll = 0, hh = 0; // Map each mm value to its corresponding (width, length, height) tuple var dimensionLookup = new Dictionary<int, (int w, int l, int h)> { { 1, (4, 8, 8) }, { 4, (4, 8, 8) }, { 7, (4, 8, 8) }, { 2, (8, 4, 8) }, { 5, (8, 4, 8) }, { 8, (8, 4, 8) }, { 3, (8, 8, /* add your missing hh value here */) }, { 6, (8, 8, /* add your missing hh value here */) }, { 9, (8, 8, /* add your missing hh value here */) } }; // Grab the dimensions if mm is in our lookup table if (dimensionLookup.TryGetValue(mm, out var dims)) { ww = dims.w; ll = dims.l; hh = dims.h; } // Initialize the array and populate it only if dimensions are valid int[, ,] to3dimen = new int[ww, ll, hh]; if (ww > 0 && ll > 0 && hh > 0) { to3dimen = To3D(m, ww, ll, hh); } return to3dimen; }
Optimized Approach 2: Switch Expression (C# 8.0+)
If you prefer a more concise, readable approach, switch expressions are perfect here—they directly map mm values to dimensions without extra boilerplate:
private int[, ,] dim(int[] m) { // Use a switch expression to get dimensions in one line (int ww, int ll, int hh) = mm switch { 1 or 4 or 7 => (4, 8, 8), 2 or 5 or 8 => (8, 4, 8), 3 or 6 or 9 => (8, 8, /* fill in your missing hh value */), _ => (0, 0, 0) // Default case for unexpected mm values }; // Set up the array and call To3D only if dimensions are valid int[, ,] to3dimen = new int[ww, ll, hh]; if (ww != 0 && ll != 0 && hh != 0) { to3dimen = To3D(m, ww, ll, hh); } return to3dimen; }
Why These Are Better
- Less Repetition: No more copying the same array setup or
To3Dcalls—all logic happens in one place. - Easier to Maintain: If you need to update a dimension (e.g., change 4x8x8 to 4x16x8), you only modify one line instead of three.
- Safer: Both approaches handle unexpected
mmvalues gracefully with default dimensions, and we only callTo3Dwhen we have valid non-zero dimensions. - Scalable: Adding new
mmvalues is as simple as adding a new entry to the dictionary or a new case in the switch expression.
内容的提问来源于stack exchange,提问作者Yussra
相关产品推荐
相关产品推荐

