Can an interface have static variables in C#

12,420

Solution 1

No, an interface in C# can't declare fields at all. You can't declare a static interface at all in C#, nor can you declare static members within an interface.

As per section 11.2 of the C# specification:

An interface declaration may declare zero or more members. The members of an interface must be methods, properties, events, or indexers. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types, nor can an interface contain static members of any kind.

All interface members implicitly have public access. It is a compile-time error for interface member declarations to include any modifiers. In particular, interfaces members cannot be declared with the modifiers abstract, public, protected, internal, private, virtual, override, or static.

Solution 2

An interface is a contract, ie a description of the public instance methods and properties that any implementing class must provide.

Interfaces cannot specify any static methods or properties. They cannot specify internal, protected or private methods or properties. Nor can they specify fields.

Share:
12,420
Jasmine
Author by

Jasmine

Updated on June 09, 2022

Comments

  • Jasmine
    Jasmine almost 2 years

    It might be a silly question, I appreciate if someone can help me understand it.

    1. Can an interface in C# can have static variables?

    2. If the interface itself need to be static to declare static variables inside?

    3. How the implementation goes for static variables(Or say property) within an interface, when we implement in a class?

    Some examples and perspicuous explanation would be greatly appreciated.

  • Charleh
    Charleh over 10 years
    There is - you can use a non-generic interface as a marker for a generic interface by having the generic implement the non-generic. In this case the marker interface doesn't require any members, there are probably other uses but a marker interface is useful in reflection scenarios
  • Uwe Keim
    Uwe Keim over 10 years
    @Alireza Yes.
  • Charleh
    Charleh over 10 years
    Here is an example in the popular MVVM framework Caliburn Micro: caliburnmicro.codeplex.com/SourceControl/latest#src/… - the interface lets reflection find types that implement the generic IHandle<T> interface without knowing which generic type to search for. Look at the nested Handler class constructor in EventAggregator to see it in action (caliburnmicro.codeplex.com/SourceControl/latest#src/…)
  • Jasmine
    Jasmine over 10 years
    Thank you Jon and others. Jon, I am new here, I am afraid I saw an article with Static Interface. It also has static property inside it :0 Please see thinkbeforecoding.com/post/2009/10/21/CSharp-Static-interfac‌​es Anyway it was an interview question for me and I said I am not sure as I need to code and check, and the interview was out lol. I still don't know why this article has static interface and I do not understand its explanation as it differs from what you have told above, sorry if I misunderstood something.