{"record":{"id":"0ccadac83258ce7c","repo":"TheAlgorithms/C-Sharp","slug":"both-points-should-have-the-same-dimensionality-manhattan","errorCode":null,"errorMessage":"Both points should have the same dimensionality","messagePattern":"Both points should have the same dimensionality","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/LinearAlgebra/Distances/Manhattan.cs","lineNumber":22,"sourceCode":"/// Implementation fo Manhattan distance.\n/// It is the sum of the lengths of the projections of the line segment between the points onto the coordinate axes.\n/// In other words, it is the sum of absolute difference between the measures in all dimensions of two points.\n///\n/// Its commonly used in regression analysis.\n/// </summary>\npublic static class Manhattan\n{\n    /// <summary>\n    /// Calculate Manhattan distance for two N-Dimensional points.\n    /// </summary>\n    /// <param name=\"point1\">First N-Dimensional point.</param>\n    /// <param name=\"point2\">Second N-Dimensional point.</param>\n    /// <returns>Calculated Manhattan distance.</returns>\n    public static double Distance(double[] point1, double[] point2)\n    {\n        if (point1.Length != point2.Length)\n        {\n            throw new ArgumentException(\"Both points should have the same dimensionality\");\n        }\n\n        // distance = |x1-y1| + |x2-y2| + ... + |xn-yn|\n        return point1.Zip(point2, (x1, x2) => Math.Abs(x1 - x2)).Sum();\n    }\n}\n","sourceCodeStart":4,"sourceCodeEnd":29,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/LinearAlgebra/Distances/Manhattan.cs#L4-L29","documentation":"Manhattan.Distance sums |x_i - y_i| across paired coordinates, which requires both arrays to have the same length. On a dimensionality mismatch it throws ArgumentException instead of allowing Zip to silently truncate the shorter point.","triggerScenarios":"Calling Manhattan.Distance(double[] point1, double[] point2) with arrays of different lengths — e.g., grid positions of differing dimension, or feature vectors built with different numbers of attributes.","commonSituations":"Pathfinding heuristics over grids where one point omits a coordinate; tabular data with missing columns; concatenating vectors from different encoders.","solutions":["Check point lengths match before calling Distance","Normalize data loading so every vector has a fixed width","Impute/pad missing coordinates before distance computation","Catch ArgumentException to flag malformed records in bulk computations"],"exampleFix":"// before\nManhattan.Distance(new double[]{1,2}, new double[]{3}); // throws\n// after\nManhattan.Distance(new double[]{1,2}, new double[]{3,0});","handlingStrategy":"validation","validationCode":"bool canCompare = point1 != null && point2 != null && point1.Length == point2.Length;","typeGuard":"bool SameDimension(double[] a, double[] b) => a.Length == b.Length;","tryCatchPattern":"try { d = Manhattan.Distance(a, b); }\ncatch (ArgumentException) { markRecordMalformed(recordId); }","preventionTips":["Validate record column counts before vectorization","Use a shared vector type that enforces fixed dimension","Impute missing coordinates instead of dropping them"],"tags":["csharp","linear-algebra","distance","dimensionality"],"backgroundTag":"shape-mismatch","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}