Reputation: 497
Is there any possibility to create an interface in order to require a virtual collection type in my class?
Regards
namespace Models.Entities
{
public partial class FileType : IMyInterface
{
public long CompanyId { get; set; }
public long FileTypeId { get; set; }
public string AcceptType { get; set; }
//IMyInterface contract
public virtual ICollection<Translation> FileTypeTranslations { get; set; }
public FileType()
{
this.FileTypeTranslations = new HashSet<FileTypeTranslation>();
}
}
public class Translation : EntityTranslation<long, FileType>
{
[Required]
public string TypeName { get; set; }
}
}
Upvotes: 1
Views: 91
Reputation:
In order to override method, property, event, indexer they must be virtual. But if they are virtual its not mandatory to override them its optional. and if we talk about abstract classes the member of abstract class is virtual implicitly. That's why we need to use override keyword when defining them in some subclass.
Upvotes: 0
Reputation: 13498
You can try to use abstract class
instead of interface
. So at classes, inherited from FileType
, you can override
this property again, i.e. behavior like with virtual
access modifier at FileType
declaration:
public abstract class MyInterface
{
public abstract ICollection<Translation> FileTypeTranslations { get; set; }
}
public class FileType : MyInterface
{
public override ICollection<Translation> FileTypeTranslations { get; set; }
}
public class FileTypeInherited : FileType
{
public override ICollection<Translation> FileTypeTranslations { get; set; }
}
Upvotes: 2
Reputation: 62258
No. virtual is an implementation detail not a contract (ie. interface) detail.
The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.
I marked the key part of that description from the documentation in bold
Upvotes: 2