Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unimplemented constructor in abstract classes

Tags:

c#

I saw a few abstract base classes .NET provides and I'm completely lost the way their constructors and virtual methods defined. For example, see System.ComponentModel.DataAnnotations.ValidationAttribute base class:

public abstract class ValidationAttribute : Attribute
{
    // Summary:
    //     Initializes a new instance of the System.ComponentModel.DataAnnotations.ValidationAttribute
    //     class.
    protected ValidationAttribute();
    ...
    protected virtual ValidationResult IsValid(object value, ValidationContext validationContext);
}

But I can't do this in my own class:

public abstract class MyClass
{
    protected MyClass();      
}

MyClass.MyClass() must declare a body because it's not marked abstract, extern, or partial

And I can't mark it abstract either:

The modifier abstract is not valid for this item

I have no specific need MyClass to be like ValidationAttribute or other base classes in .NET. I just want to know how did they compile on the runtime.

like image 832
Mike Debela Avatar asked Mar 24 '26 23:03

Mike Debela


1 Answers

You are not looking at the code of ValidationAttribute, you are looking at its metadata.

The actual constructor looks like

protected ValidationAttribute()
    : this(() => DataAnnotationsResources.ValidationAttribute_ValidationError) {
}
like image 115
Scott Chamberlain Avatar answered Mar 27 '26 13:03

Scott Chamberlain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!