{"record":{"id":"dc6cc97c09983771","repo":"dotnet/maui","slug":"default-value-did-not-match-return-type-property","errorCode":null,"errorMessage":"Default value did not match return type. Property: {returnType} {declaringType.Name}.{propertyName} Default value type: {defaultValue.GetType().Name}, ","messagePattern":"Default value did not match return type\\. Property: (.+?) (.+?)\\.(.+?) Default value type: (.+?), ","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/BindableProperty.cs","lineNumber":209,"sourceCode":"\t\t{\n\t\t\tif (propertyName == null)\n\t\t\t\tthrow new ArgumentNullException(nameof(propertyName));\n\t\t\tif (returnType is null)\n\t\t\t\tthrow new ArgumentNullException(nameof(returnType));\n\t\t\tif (declaringType is null)\n\t\t\t\tthrow new ArgumentNullException(nameof(declaringType));\n\t\t\t\n\t\t\tInternalId = Interlocked.Increment(ref _nextInternalId);\n\n\t\t\t// don't use Enum.IsDefined as its redonkulously expensive for what it does\n\t\t\tif (defaultBindingMode != BindingMode.Default && defaultBindingMode != BindingMode.OneWay && defaultBindingMode != BindingMode.OneWayToSource && defaultBindingMode != BindingMode.TwoWay && defaultBindingMode != BindingMode.OneTime)\n\t\t\t\tthrow new ArgumentException($\"Not a valid type of BindingMode. Property: {returnType} {declaringType.Name}.{propertyName}. Default binding mode: {defaultBindingMode}\", nameof(defaultBindingMode));\n\n\t\t\tif (defaultValue == null && Nullable.GetUnderlyingType(returnType) == null && returnType.IsValueType)\n\t\t\t\tdefaultValue = Activator.CreateInstance(returnType);\n\n\t\t\tif (defaultValue != null && !returnType.IsInstanceOfType(defaultValue))\n\t\t\t\tthrow new ArgumentException($\"Default value did not match return type. Property: {returnType} {declaringType.Name}.{propertyName} Default value type: {defaultValue.GetType().Name}, \", nameof(defaultValue));\n\n\t\t\tif (defaultBindingMode == BindingMode.Default)\n\t\t\t\tdefaultBindingMode = BindingMode.OneWay;\n\n\t\t\tPropertyName = propertyName;\n\t\t\tReturnType = returnType;\n\t\t\tDeclaringType = declaringType;\n\t\t\tDefaultValue = defaultValue;\n\t\t\tDefaultBindingMode = defaultBindingMode;\n\t\t\tPropertyChanged = propertyChanged;\n\t\t\tPropertyChanging = propertyChanging;\n\t\t\tValidateValue = validateValue;\n\t\t\tCoerceValue = coerceValue;\n\t\t\tBindingChanging = bindingChanging;\n\t\t\tIsReadOnly = isReadOnly;\n\t\t\tDefaultValueCreator = defaultValueCreator;\n\t\t}\n","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/BindableProperty.cs#L191-L227","documentation":"ArgumentException thrown by the BindableProperty constructor when the supplied default value is non-null but is not an instance of the declared returnType. The constructor first fills in a default for non-nullable value types via Activator.CreateInstance, then checks assignability here; a mismatch means the property would carry a value of the wrong type, so it fails fast with the property identity and the actual default-value type.","triggerScenarios":"BindableProperty.Create(nameof(X), typeof(int), typeof(V), \"0\") (string default for an int property); Create(typeof(Color), typeof(V), Brushes.Red) where the value is a related but distinct type; passing an enum value when returnType is the underlying int.","commonSituations":"Changing a property's returnType after authoring defaults; XAML/compiled defaults where the literal type doesn't match; box/unbox mismatches with nullable value types.","solutions":["Make the default value's runtime type assignable to returnType (e.g. pass 0, not \"0\", for an int).","If returnType changed, update every Create call site's default literal.","For nullable value types pass null rather than a typed default."],"exampleFix":"// before\npublic static readonly BindableProperty CountProperty =\n    BindableProperty.Create(nameof(Count), typeof(int), typeof(V), \"0\");\n\n// after\npublic static readonly BindableProperty CountProperty =\n    BindableProperty.Create(nameof(Count), typeof(int), typeof(V), 0);","handlingStrategy":"validation","validationCode":"if (defaultValue is not null && !returnType.IsInstanceOfType(defaultValue))\n    throw new ArgumentException(\"default value type mismatch\", nameof(defaultValue));\nvar bp = BindableProperty.Create(nameof(X), returnType, typeof(V), defaultValue);","typeGuard":"static bool DefaultMatches(Type returnType, object defaultValue) =>\n    defaultValue is null || returnType.IsInstanceOfType(defaultValue);","tryCatchPattern":null,"preventionTips":["Use a literal of the exact runtime type for the default.","When changing returnType, sweep every Create call site's default literal."],"tags":["maui","bindableproperty","argument-validation","type-mismatch","default-value"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}