"What is why they are in this particular order?"
The order is:
Display Name
MaximumLength
MinimumLength
The answer on question string frormat is in FormatErrorMessage
method of StringLengthAttribute.cs
:
public override string FormatErrorMessage(string name) {
this.EnsureLegalLengths();
bool useErrorMessageWithMinimum = this.MinimumLength != 0 && !this.CustomErrorMessageSet;
string errorMessage = useErrorMessageWithMinimum ?
DataAnnotationsResources.StringLengthAttribute_ValidationErrorIncludingMinimum : this.ErrorMessageString;
// it's ok to pass in the minLength even for the error message without a {2} param since String.Format will just
// ignore extra arguments
return String.Format(CultureInfo.CurrentCulture, errorMessage, name, this.MaximumLength, this.MinimumLength);
}
"What would be if I remove attribute [Display(Name = "My property")]? Does {0} in that case just takes name of my property "MyProperty":
Yes it would use member name.
Here is a code of DisplayName property which is passed as name
to FormatErrorMessage
:
public string DisplayName {
get {
if (string.IsNullOrEmpty(this._displayName)) {
this._displayName = this.GetDisplayName();
if (string.IsNullOrEmpty(this._displayName)) {
this._displayName = this.MemberName;
if (string.IsNullOrEmpty(this._displayName)) {
this._displayName = this.ObjectType.Name;
}
}
}
return this._displayName;
}
}