473,394 Members | 1,674 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,394 software developers and data experts.

C# Interfaces and static methods

4
Hi,
I have recently started writing in C# on Linux using mono and come from a C++ background.

I would like to create interfaces that expose methods for objects constructed using the singleton and factory method patterns. Both of these typically use a static method to return an object that has been constructed.

Is there any way of doing this using C# interfaces? or do I need to use an abstract base class?

My implementation looks something like this:

Expand|Select|Wrap|Line Numbers
  1. interface IDisplay
  2. {
  3.     // some property accesses and methods
  4. }
  5.  
  6. public class CDisplay: IDisplay
  7. {
  8.     static CDisplay s_instance = null;
  9.     public static IDisplay instance {
  10.         get {
  11.             if (null == s_instance)
  12.                 s_instance = new CDisplay();
  13.             return s_instance;
  14.         }
  15.     }
  16.  
  17.     // implement constructor and interface
  18. }
  19.  
The problem with this implementation is that I need to expose CDisplay to my calling code when I think that it should be hidden as private or internal.

Is there a way of adding static methods to interfaces?

thanks

dan
Nov 28 '07 #1
5 2346
r035198x
13,262 8TB
Hi,
I have recently started writing in C# on Linux using mono and come from a C++ background.

I would like to create interfaces that expose methods for objects constructed using the singleton and factory method patterns. Both of these typically use a static method to return an object that has been constructed.

Is there any way of doing this using C# interfaces? or do I need to use an abstract base class?

My implementation looks something like this:

Expand|Select|Wrap|Line Numbers
  1. interface IDisplay
  2. {
  3.     // some property accesses and methods
  4. }
  5.  
  6. public class CDisplay: IDisplay
  7. {
  8.     static CDisplay s_instance = null;
  9.     public static IDisplay instance {
  10.         get {
  11.             if (null == s_instance)
  12.                 s_instance = new CDisplay();
  13.             return s_instance;
  14.         }
  15.     }
  16.  
  17.     // implement constructor and interface
  18. }
  19.  
The problem with this implementation is that I need to expose CDisplay to my calling code when I think that it should be hidden as private or internal.

Is there a way of adding static methods to interfaces?

thanks

dan
Interfaces cannot have static members.
Your problem is probably because your interface is not public but you are trying to return an object of that interface type in a public context.
Nov 28 '07 #2
dans
4
Interfaces cannot have static members.
Your problem is probably because your interface is not public but you are trying to return an object of that interface type in a public context.
The interface is public.
Nov 28 '07 #3
Plater
7,872 Expert 4TB
Could you do something like this:

Expand|Select|Wrap|Line Numbers
  1. public static class ContainerClass
  2. {
  3.    private interface iMyInterface
  4.    {
  5.       void somefunc();
  6.    }
  7.    private class MyClass : iMyInterface
  8.    {
  9.       //do stuff
  10.       //like implement the interface's stuff
  11.    }
  12.  
  13.    public static MyClass CreateAMyClass()
  14.    {
  15.       return new MyClass();
  16.    }
  17. }
  18.  
Then your class and interface are "hidden" (sort of) but you can still get a reference to one of them?

Or, replace "public static MyClass CreateAMyClass()" with:
Expand|Select|Wrap|Line Numbers
  1. public static MyClass TheOnlyValidMyClass = new MyClass();
  2.  
Then reference with:
Expand|Select|Wrap|Line Numbers
  1. ContainerClass.TheOnlyValidMyClass 
  2.  
?
Nov 28 '07 #4
r035198x
13,262 8TB
The interface is public.
No it is not. Look at the code you posted above. There is no public modifier for the interface.


Here is a lazy implementation which has the interface declared as public. You'll notice that created is only printed once.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. public interface IDisplay
  3. {
  4. // some property accesses and methods
  5. }
  6.  
  7. public class CDisplay: IDisplay {
  8.     static IDisplay s_instance = null;
  9.     public static IDisplay instance {
  10.         get {
  11.             if (null == s_instance)
  12.                 s_instance = new CDisplay();
  13.  
  14.             return s_instance;
  15.         }
  16.     }
  17.     private CDisplay() {
  18.         Console.WriteLine("created");
  19.     } 
  20.     public static void Main(String[] args ) {
  21.         IDisplay test = CDisplay.instance;
  22.         IDisplay test2 = CDisplay.instance;
  23.     }
  24.  
  25. // implement constructor and interface
  26. }
Nov 29 '07 #5
dans
4
No it is not. Look at the code you posted above. There is no public modifier for the interface.


Here is a lazy implementation which has the interface declared as public. You'll notice that created is only printed once.
Sorry, I should have said that the interface is public in my code. Is there a way of editing previous posts?

Your implementation is pretty much what I have come up with, except that CDisplay does not implement Main.

What I'm really trying to avoid is exposing any of CDisplay to the caller as it is being implemented as a separate assembly and only the interface should be exposed. I could have something like:

Expand|Select|Wrap|Line Numbers
  1. public interface IDisplay
  2. {
  3.     // interface properties and methods
  4. }
  5.  
  6. public class CDisplay : IDisplay
  7. {
  8.     public static IDisplay instance {
  9.         get {
  10.             return CDisplayImplementation.instance;
  11.         }
  12.     }
  13.     // abstract class implementing interface and providing static instance method.
  14. }
  15.  
  16. // class internal to allow other parts of the assembly to access it but prevent access from outside the assembly.
  17. internal class CDisplayImplementation : CDisplay
  18. {
  19.     private static CDisplay s_instance = null;
  20.     public static IDisplay instance {
  21.         get {
  22.             if (null == s_instance)
  23.                 s_instance = new CDisplay();
  24.             return s_instance;
  25.         }
  26.     }
  27.     // concrete implementation
  28.  
  29. }
  30.  
but by this seems unnecessarily complicated and I would be inclined to just expose my interface as an abstract class unless I want to inherit more than one interface.
Nov 29 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Franco Gustavo | last post by:
Hi, Please help me to understand this, because I don't see what I'm missing. I was reading a lot of examples on Internet that explain that C# doesn't implement multiple inheritance it...
1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
7
by: mdc | last post by:
Hi, Is there any way to implement an interface as a static method in C#? If not, is this a bug? Micheal.
12
by: Peter N Roth | last post by:
If i build a class in C#, i can declare a method abstract. If someone derives from this class, the abstract method must be overriden for the computation to succeed. ISTM that an Interface...
5
by: Andy | last post by:
Why can't I create an interface and make its implementation static/shared? Is there some pattern that lets me work around this? Any explanation of why this is would be appreciated. This...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
42
by: redefined.horizons | last post by:
I'm coming from a Java background, so please don't stone me... I see that Python is missing "interfaces". The concept of an interface is a key to good programming design in Java, but I've read...
6
by: WXS | last post by:
I know this sounds contrary to the idea of an interface, but read this and see what you think. ----------------------------------------------------------------------------------------- It would be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.