Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Interface implemented by empty abstract class

Can I leave an abstract class that implements interfaces empty and imply that all the methods/properties in the interface are abstract within my class. It appears that I have to write them out again in the abstract class but I really want to avoid this duplication.

My reason is I have a couple of interfaces with different accessors, one public and one internal, that I want to bring together so I have an abstract class that implements them both that can then be extended.

public interface ISomePublicProperties {
    int PropertyOne {get;}
}

internal interface ISomeInternalProperties {
    int PropertyTwo {get;}
}

public abstract class SomeClass : ISomePublicProperties, ISomeInternalProperties {}

But the compiler complains that SomeClass does not implement interface method ISomePublicProperties.PropertyOne and ISomeInternalProperties.PropertyTwo

Is there anyway in C# (I know Java allows this) that I can leave the abstract class empty implementing interfaces?

like image 286
kevin Avatar asked Mar 31 '11 03:03

kevin


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

Nope. In C# the abstract class must fully implement the interface. Note it can implement it with abstract methods and abstract properties. It's one little thing about C# that has always bugged me.

like image 91
Matt Greer Avatar answered Oct 30 '22 01:10

Matt Greer


The only way to do it is create virtual properties with no implementation. These are then overridden in your derived classes.

public abstract class SomeClass : ISomePublicProperties, ISomeInternalProperties 
{
    public abstract int PropertyOne { get; }
    internal abstract int PropertyTwo { get; }
}
like image 45
Andrew Cooper Avatar answered Oct 30 '22 00:10

Andrew Cooper