Question about interfaces


M

mortb

Why can't you place static methods in an interface?
For example, if I want an uniform manner to create instances of classes that
implement my interface I'd like to put a static factory mehtod in the
interface. However, this is not possible as the interface does not allow
static memebers.

Is there a good reason for this design?

/mortb
 
Ad

Advertisements

G

Greg Young

Actually you can (in IL).

I would imagine it was done this way in most of the .net langauges as it is
a bit of a confusing concept ... I can understand the logic of treating an
interface as a special type which can only represent a contract.

Cheers,

Greg Young
MVP - C#
 
G

Guest

Interface only provides you template and it is independed of scope and access
modifiers and implementation details. Specifiying something is Implementation
Detail. For this purpose you can use Abstract Classes.
 
P

Peter Morris [Droopy eyes software]

For example, if I want an uniform manner to create instances of classes
that implement my interface
For this you should use the factory pattern.
 
Ad

Advertisements

I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

An Interface is intented to be used only as a contract, with no
implementation at all. If you can define a static member you must provide an
implementation for it, thus breaking the no-code feature. Even more you
could need a static constructor (as a method that is called only once ,
before any other static member) so you ends with a class , or a class-like
entity

If you want to provide a Factory , simply use a factory class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top