{"record":{"id":"ba7aa9984edff36a","repo":"dotnet/wpf","slug":"error-multiple-main-methods-in-class-0","errorCode":null,"errorMessage":"Error: Multiple Main methods in class '{0}'","messagePattern":"Error: Multiple Main methods in class '(.+?)'","errorType":"exception","errorClass":"CspProjectException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/tools/csp/Project.cs","lineNumber":314,"sourceCode":"\n\n        /// <summary>\n        /// Tests whether the type has a Main method.\n        /// Throws an exception if more than one is present.\n        /// </summary>\n        private bool IsMainPresent(Type t)\n        {\n            MethodInfo method = null;\n            try\n            {\n                method = t.GetMethod(\n                    \"Main\",\n                    BindingFlags.Public | BindingFlags.Static\n                    );\n            }\n            catch (System.Reflection.AmbiguousMatchException)\n            {\n                throw new CspProjectException(\n                    \"Error: Multiple Main methods in class '\" + t.FullName + \"'\");\n            }\n\n            return method != null;\n        }\n\n        /// <summary>\n        /// Report an exception to the console.\n        /// </summary>\n        private static void ReportException(Exception e)\n        {\n            throw new ApplicationException(e.Message);\n        }\n\n\n        #endregion Private Methods\n\n","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/tools/csp/Project.cs#L296-L332","documentation":"IsMainPresent (Project.cs:302) calls Type.GetMethod(\"Main\", Public|Static), which throws AmbiguousMatchException when a single class declares more than one public static Main overload. The tool treats this as fatal and rethrows it as CspProjectException naming the offending class, because it cannot disambiguate the entry point.","triggerScenarios":"The assembly contains a class with two or more public static methods named 'Main' (e.g. static void Main(string[]) and static int Main()) so Type.GetMethod cannot return a single MethodInfo.","commonSituations":"Hand-written overloads of Main for testing, copy-pasted Main signatures in the startup class, conditional-compilation or refactoring leftovers that left two entry-point-shaped methods in one class.","solutions":["Keep exactly one public static Main per class; delete or rename the duplicate overload (e.g. to MainInternal).","Reduce visibility of the unwanted overload (non-public or non-static) so it no longer matches Public|Static binding flags.","Use #if directives to compile only one Main overload per build configuration."],"exampleFix":"// before\npublic static void Main(string[] args) { Run(args); }\npublic static int Main() { Run(new string[0]); return 0; }\n\n// after\npublic static void Main(string[] args) { Run(args); }\nprivate static int MainNoArgs() { Run(new string[0]); return 0; }","handlingStrategy":"validation","validationCode":"// Detect classes with ambiguous Main overloads before execution\nvar bad = assembly.GetTypes().Where(t =>\n    t.GetMethods(BindingFlags.Public | BindingFlags.Static)\n     .Count(m => m.Name == \"Main\") > 1).ToList();\nif (bad.Count > 0)\n    throw new InvalidOperationException(\"Ambiguous Main overloads in: \" + string.Join(\",\", bad.Select(t => t.FullName)));","typeGuard":null,"tryCatchPattern":"try { project.ExecuteMain(); }\ncatch (CspProjectException ex) when (ex.Message.StartsWith(\"Error: Multiple Main methods in class\")) {\n    Console.Error.WriteLine($\"Remove duplicate Main overloads: {ex.Message}\");\n}","preventionTips":["Never overload the name Main in the startup class; use distinct method names.","Guard alternate entry points behind #if so only one compiles per configuration.","Keep extra entry-point-shaped methods private or non-static so they don't match Public|Static binding flags."],"tags":["reflection","entry-point","overload","ambiguous-match"],"backgroundTag":"conflicting-config-options","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}