473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question about interfaces

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
May 10 '06 #1
4 1016
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#
"mortb" <mo***@nospam.nospam> wrote in message
news:uO*************@TK2MSFTNGP02.phx.gbl...
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

May 10 '06 #2
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.

"mortb" wrote:
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

May 10 '06 #3
> 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.
May 10 '06 #4
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.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"mortb" <mo***@nospam.nospam> wrote in message
news:uO*************@TK2MSFTNGP02.phx.gbl...
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

May 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lloyd Sheen | last post by:
I have the following in one form: Public Class ExeScript Inherits System.Windows.Forms.UserControl Implements ToadInterfaces.Interfaces.GotFocus, ToadInterfaces.Interfaces.HasConfiguration,...
5
by: Geoff Pennington | last post by:
Is there an easy way to find out what classes implement a given interface? For example, it might be useful to know what classes implement ICollectible. Much obliged, Geoff.
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
3
by: John Underwood | last post by:
Hi.. I was looking at interface, and I have a example in the docs i'll paste below.. I'm not grasping what you would gain by using a interface, does any one have a brief description of their...
7
by: tshad | last post by:
I am trying to understand why I would use interfaces. In the following example for IPrinciple, I have the following code: ************************************************************ using...
4
by: denis | last post by:
Hi All, I'm making a class which derives(implements) from 5 interfaces: class myClass: public base1, public base2, public base3,....... I want to be able to ask from any interface about a...
1
by: Larry | last post by:
I checked definition of class CollectionBase public abstract class CollectionBase : IList, ICollection, IEnumerable, it implements 3 interface IList, ICollection and IEnumerable. I found...
1
by: brianlanning | last post by:
I have a class that represents a business object. Currently, this class exists as a vb6 "class" which will be ported to .net. The plan is to separate this out into a .net object residing in a web...
2
by: parez | last post by:
Layer A is on top on Layer B. Layer A talks to layer B using the interfaces defined in B. For input and return values. My question is should i take out the interfaces from B and put it in a...
6
by: S_K | last post by:
Hi, I've been toying around with interfaces in C#. They are fun but can anybody give me some examples of where interfaces are used and what they are used for? Thanks so much. Steve
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.