{"record":{"id":"f95563b2452d82de","repo":"devlooped/moq","slug":"resources-invalidcallbacknotadelegatewithreturntypevoid","errorCode":null,"errorMessage":"Resources.InvalidCallbackNotADelegateWithReturnTypeVoid","messagePattern":"Resources\\.InvalidCallbackNotADelegateWithReturnTypeVoid","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/MethodCall.cs","lineNumber":178,"sourceCode":"            }\n            else\n            {\n                var expectedParamTypes = this.Method.GetParameterTypes();\n                if (!callback.CompareParameterTypesTo(expectedParamTypes))\n                {\n                    throw new ArgumentException(\n                        string.Format(\n                            CultureInfo.CurrentCulture,\n                            Resources.InvalidCallbackParameterMismatch,\n                            this.Method.GetParameterTypeList(),\n                            callback.GetMethodInfo().GetParameterTypeList()));\n                }\n\n                var callbackMethod = callback.GetMethodInfo();\n\n                if (callbackMethod.ReturnType != typeof(void))\n                {\n                    throw new ArgumentException(Resources.InvalidCallbackNotADelegateWithReturnTypeVoid, nameof(callback));\n                }\n\n                if (callbackMethod.GetParameterTypes().Any(Extensions.IsOrContainsTypeMatcher))\n                {\n                    throw new ArgumentException(Resources.TypeMatchersMayNotBeUsedWithCallbacks);\n                }\n\n                behavior = new Callback(invocation => callback.InvokePreserveStack(invocation.Arguments));\n            }\n        }\n\n        public void SetFailMessage(string failMessage)\n        {\n            this.failMessage = failMessage;\n        }\n\n        public void SetRaiseEventBehavior<TMock>(Action<TMock> eventExpression, Delegate func)\n            where TMock : class","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/MethodCall.cs#L160-L196","documentation":"SetCallbackBehavior throws this ArgumentException when a callback delegate used on a setup that returns a value has a non-void return type. Callbacks must be Action-like (return void); Func delegates with return values are rejected — use Returns for values.","triggerScenarios":"Passing a Func (e.g. `Callback(() => someValue)`) or a method group returning a value to .Callback() on a setup.","commonSituations":"Developers confusing Callback with Returns; reusing an existing Func delegate as a callback; expression-bodied members that return values.","solutions":["Use a void-returning lambda/Action for Callback()","Move the value computation to Returns() if a return value is needed","If the callback should produce the return value, use Returns with a matching Func"],"exampleFix":"// before\nmock.Setup(m => m.GetValue()).Callback(() => ComputeValue());\n// after\nmock.Setup(m => m.GetValue()).Returns(() => ComputeValue());","handlingStrategy":"validation","validationCode":"if (callback.GetMethodInfo().ReturnType != typeof(void)) throw new InvalidOperationException(\"Callback must return void; use Returns for values\");","typeGuard":"static bool IsVoidCallback(Delegate d) => d.GetMethodInfo().ReturnType == typeof(void);","tryCatchPattern":"try { setup.Callback(cb); } catch (ArgumentException ex) when (ex.Message.Contains(\"void\")) { /* use Returns instead */ }","preventionTips":["Only pass Action delegates (or void lambdas) to Callback()","Use Returns for delegates that produce values","Avoid passing Func method groups to Callback"],"tags":["moq","callback","return-type","action-func"],"backgroundTag":"type-mismatch","analyzedSha":"89a5be629c752960fe403e95ff583e4ae6f00542","analyzedAt":"2026-09-16T05:31:39.496Z","contentChangedAt":"2026-09-16T05:31:39.496Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}