{"record":{"id":"3bb3c7b6d5b93bcd","repo":"TheAlgorithms/C-Sharp","slug":"both-points-should-have-the-same-dimensionality-euclidean","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/Euclidean.cs","lineNumber":18,"sourceCode":"namespace Algorithms.LinearAlgebra.Distances;\n\n/// <summary>\n/// Implementation for Euclidean distance.\n/// </summary>\npublic static class Euclidean\n{\n    /// <summary>\n    /// Calculate Euclidean 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 Euclidean 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 = sqrt((x1-y1)^2 + (x2-y2)^2 + ... + (xn-yn)^2)\n        return Math.Sqrt(point1.Zip(point2, (x1, x2) => (x1 - x2) * (x1 - x2)).Sum());\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":25,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/LinearAlgebra/Distances/Euclidean.cs#L1-L25","documentation":"Euclidean.Distance computes sqrt of the sum of squared coordinate differences, requiring both points to share dimensionality. If point1.Length != point2.Length it throws ArgumentException rather than silently ignoring coordinates via Zip.","triggerScenarios":"Calling Euclidean.Distance(double[] point1, double[] point2) with arrays of differing lengths — e.g., comparing a 2D coordinate to a 3D coordinate, or feature vectors of unequal width.","commonSituations":"KNN/clustering code fed vectors from inconsistent feature extractors; schema changes adding a feature to one dataset only; mixing latitude/longitude pairs with x/y/z points.","solutions":["Assert equal array lengths before invoking Distance","Align feature pipelines so all vectors share the same dimension count","Pad missing features with defaults before comparison","Catch ArgumentException and log/skip the mismatched vector pair"],"exampleFix":"// before\nEuclidean.Distance(new double[]{0,0}, new double[]{1,1,1}); // throws\n// after\nvar p = new double[]{0,0,0};\nvar q = new double[]{1,1,1};\nEuclidean.Distance(p, q);","handlingStrategy":"validation","validationCode":"if (point1.Length != point2.Length) throw new ArgumentException(\"Dimension mismatch for Euclidean distance\");","typeGuard":"bool SameDimension(double[] a, double[] b) => a.Length == b.Length;","tryCatchPattern":"try { d = Euclidean.Distance(a, b); }\ncatch (ArgumentException) { skipOrLogPair(a, b); }","preventionTips":["Assert vector widths in dataset loaders","Pad/truncate vectors in preprocessing to a canonical dimension","Unit-test distance calls with fixed-width fixtures"],"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"}