Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an empty interface used for?

Tags:

c#

oop

interface

The code I'm working with has an empty interface:

public interface ICube {}

It has no methods or properties.

Some classes implement ICube and other interfaces inherit from ICube.
Please someone tell me how that ICube interface could be beneficial?

like image 965
kamran186 Avatar asked Apr 19 '19 12:04

kamran186


People also ask

What is the use of empty interface in C#?

An empty interface does not define any members. Therefore, it does not define a contract that can be implemented. If your design includes empty interfaces that types are expected to implement, you are probably using an interface as a marker or a way to identify a group of types.

What is the purpose of an interface?

The purpose of interfaces is to allow the computer to enforce these properties and to know that an object of TYPE T (whatever the interface is ) must have functions called X,Y,Z, etc.

Which of the following interface is empty?

It is an empty interface (no field or methods). Examples of marker interface are Serializable, Cloneable and Remote interface.

Can we declare empty interface in Java?

Implementing an empty interface tells the compiler to do some operations. It is used to logically divide the code and a good way to categorize code. It is more useful for developing API and in frameworks like Spring.


1 Answers

You should read from MSDN about Interface Design

https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/interface

✓ DO define an interface if you need some common API to be supported by a set of types that includes value types.

✓ CONSIDER defining an interface if you need to support its functionality on types that already inherit from some other type.

X AVOID using marker interfaces (interfaces with no members).

If you need to mark a class as having a specific characteristic (marker), in general, use a custom attribute rather than an interface.

✓ DO provide at least one type that is an implementation of an interface.

Doing this helps to validate the design of the interface. For example, List is an implementation of the IList interface.

✓ DO provide at least one API that consumes each interface you define (a method taking the interface as a parameter or a property typed as the interface).

Doing this helps to validate the interface design. For example, List.Sort consumes the System.Collections.Generic.IComparer interface.

X DO NOT add members to an interface that has previously shipped.

More reference: Empty interface Are empty interfaces code smell?

Marker interface What is the purpose of a marker interface?

like image 105
Hien Nguyen Avatar answered Oct 05 '22 22:10

Hien Nguyen