Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: interface inheritance getters/setters

I have a set of interfaces which are used in close conjunction with particular mutable object.

Many users of the object only need the ability to read values from the object, and then only a few properties. To avoid namespace pollution (easier intellisense) and to get across the usage intent, I'd like to have a small base interface which only exposes a few "key" properties in a read-only fashion.

However, almost all implementations will support the full interface, which includes modifiability.

Unfortunately, I ran into a roadblock expressing that concept in C#:

interface IBasicProps {    public int Priority { get; }    public string Name {get;}    //... whatever }  interface IBasicPropsWriteable:IBasicProps  {    public int Priority { set; } //warning CS0108: [...] hides inherited member [...]    public string Name { set; }    //... whatever } 

I certainly wasn't intending to hide any members, so that aint good!

Of course, I can solve this using methods just fine, but what's the right choice? I'd like to keep the "core" interface as small as possible even if splitting the interfaces serves no purpose other than communicating intent. With split interfaces, it's just really obvious which methods aren't going to do any updating, and it makes writing code a bit clearer (not to mention also allows nice-n-simple static singleton stubs that suffice for quite a few simple cases).

I'd like to avoid any abstract classes and the like; they make reimplementation or quick single-purpose shims all that more complex and hard-to-grok.

So, ideas?

like image 806
Eamon Nerbonne Avatar asked Nov 24 '09 16:11

Eamon Nerbonne


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. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 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.


1 Answers

Method hiding in an interface isn't nearly as grungy; I'd go with something like:

interface IBasicProps {    int Priority { get; }    string Name {get;}    //... whatever }  interface IBasicPropsWriteable:IBasicProps  {    new int Priority { get; set; }    new string Name { get; set; }    //... whatever } class Foo : IBasicPropsWriteable {     public int Priority {get;set;}     public string Name {get;set;} /* optional     int IBasicProps.Priority {get {return Priority;}}     string IBasicProps.Name {get {return Name;}} */ } 
like image 130
Marc Gravell Avatar answered Sep 23 '22 03:09

Marc Gravell