Doesn't that violate the interface design principle in C #?

When I googled to find related topics about the interface, I found this on the MSDN website:

For example, an interface may declare a property that has access access. A class that implements an interface can declare the same property with either get or a set of accessories. from MSDN

Now I have doubts. When we specifically mentioned that the property should be read-only (only the "get" accessor in the interface), why is it also allowed to implement the "set" accessor?

+4
source share
10 answers

Now I have doubts. When we specifically mentioned that the property should be read-only (only the "get" accessor in the interface), why is it also allowed to implement the "set" accessor?

There is a difference - when you use the interface, you are not "indicating that the property should be read-only", but rather indicating that the contract defines the "readable property" of this particular name and type. Basically, the interface defines the minimum requirements for a contract, not the absolute requirements.

If you pass an object to a specific interface, the property setting tool will not be available. This really is no different from having additional properties or methods for an object that are not accessible through the interface.

+12
source

You cannot access the set property from an interface link, so it does not matter if it was implemented or not when the interface was opened to the public.

Of course, sometimes it is necessary to implement a set accessor on the class side, that is, when working with a class that allows access to classes that are in the same assembly.

+3
source

An interface is a minimal set of requirements that must be implemented, but you can implement more. In this case, the read-write property is more than read-only.

In addition to additional contract terms, you can add any other methods and / or properties, as well as implement other interfaces in the same class.

+3
source

Code that uses the interface does not know that the set exists and therefore cannot use it.

+2
source

Think of the interface as a contract. Contractors promise to at least comply with the behavior defined in this contract, but are not limited to this. Interfaces allow components to communicate without tight communication. Therefore, an implementation can accept either get or set , but at least it must adhere to get .

0
source

The class satisfies the requirements of the interface; everything else is a detail of the implementation of the class itself. If you access an object through an interface, you will only see it. So, no, that doesn't really break it, as intended.

0
source

An interface is simply a declaration of how an object should be used by consumers. It does not contain any implementation specifications. There is no contradiction.

0
source
 public interface IFoo { string Name { get; } } class FooImplementation : IFoo { public string Name { get; set; } } public class FooWorker { public void WorkOnFoo(IFoo foo) { if (null == foo) throw new ArgumentNullException("foo"); Console.WriteLine(foo.Name); } } public class Program { public void Main() { IFoo foo = new FooImplementation { Name = "Foo" }; new FooWorker().WorkOnFoo(foo); } } 

For FooWorker , the foo parameter has a get attribute on the Name property.

It is probably important to remember that the Name property can still be set to foo through reflection or cast.

0
source

What Felix said is true.

In more detail, an interface defines a minimal set of functions that must exist on any object defined as an implementation of the interface. This provides a β€œcommon” set of functionality for all implementations of an interface, so you know that if an object implements an interface, you can call it X, Y, and Z. For example, because something is IDisposable, for example, it does not mean that ALL the object can do. In fact, this would make the interfaces pretty pointless if they also defined maximum functionality. This is all you need if and when you work with an object as an implementation of an interface; if all you need is IDisposable, you only need to call Dispose (), no matter what additional members may have a particular implementation of IDisposable.

Back to your example, the interface that defines the property indicates that it should have open access to accessories. He does not and cannot say that he cannot have access to the public set; it's just the same. The kit accessory may be public, internal, secure, closed or non-existent; which interface consumers will expect, and thus what interface developers will need, is a get accessor.

0
source

It can be useful to think in terms of three types of things: the abstract ReadableFoo class (or the IReadableFoo interface), as well as the specific classes ImmutableFoo and MutableFoo (or IImmutableFoo and IChangeableFoo interfaces). Anyone who receives a parameter of type ReadableFoo will be able to read it, but will not be able to set it, and will not be able to reliably store data in it, simply by saving the link. Anyone who receives the ImmutableFoo parameter will be able to reliably save data by saving the link, but will not be able to change it. Anyone who receives the MutableFoo parameter will be able to modify the data, but not reliably save the data while maintaining the link.

0
source

Source: https://habr.com/ru/post/1398087/


All Articles