Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to describe c# interface without the constructor and with static method? [closed]

Tags:

c#

I want create interface for singleton. But interface not can ban public constructor and describ static method. how to solve this of problems?

like image 823
SkyN Avatar asked Jan 24 '26 21:01

SkyN


2 Answers

You cannot have a ISingleton interface for two reasons:

  1. Interfaces cannot have static members
  2. Interfaces cannot contain constructors

You could have a ASingleton abstract class if you liked.

like image 82
Matt Mitchell Avatar answered Jan 27 '26 11:01

Matt Mitchell


You can't, basically. Interfaces can't contain static methods, and can't put any constraints on what kind of constructors are available.

You might be interested in my notion of static interfaces, which would allow the idea of enforcing a particular static member to be present - but it wouldn't allow you to enforce the absence of a constructor.

like image 40
Jon Skeet Avatar answered Jan 27 '26 11:01

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!