Can an interface have static variables in C#
Asked Answered
T

4

8

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.

Thanatopsis answered 21/10, 2013 at 13:40 Comment(5)
An interface is only a contract and not an implementation.Vitality
Fire up your IDE and try it and/or or read Interfaces (C# Programming Guide).Tapster
you can declare it in an abstract classKnowle
variables only exist within methods. Within classes, you may have various members such as fields or properties, but you'll never have a variable. It helps to learn to use the right words to describe the right features.Madden
As of c# 8, static members of an interface are allowed. learn.microsoft.com/en-us/dotnet/csharp/language-reference/…Farthingale
C
19

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.

Consanguineous answered 21/10, 2013 at 13:41 Comment(8)
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 scenariosReboant
@Alireza Yes.Termination
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/…)Reboant
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-interfaces 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.Thanatopsis
now it seems we canTrengganu
@Coolguy: Yes, I don't go back over tens of thousands of answers every time there's a new release of C# (or other languages).Consanguineous
@JonSkeet understandable, so for the sake of completeness I'll provide an edit to the answerTrengganu
@Coolguy: No, please provide a new answer instead.Consanguineous
T
3

From C# 11 and .Net 7, you can add static members to interfaces; not only that, but you can make them abstract too

Trengganu answered 22/10, 2023 at 10:23 Comment(0)
C
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.

Catechism answered 21/10, 2013 at 13:43 Comment(0)
K
-3

1- No, because interface is not a class

2- Consider an Abstract class

3- Static Property in an interface is not defined nor is meaningful in C#

Kiarakibble answered 21/10, 2013 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.