Why Can't C# Interfaces Contain Fields

Why can't C# interfaces contain fields?

Though many of the other answers are correct at the semantic level, I find it interesting to also approach these sorts of questions from the implementation details level.

An interface can be thought of as a collection of slots, which contain methods. When a class implements an interface, the class is required to tell the runtime how to fill in all the required slots. When you say

interface IFoo { void M(); } 
class Foo : IFoo { public void M() { ... } }

the class says "when you create an instance of me, stuff a reference to Foo.M in the slot for IFoo.M.

Then when you do a call:

IFoo ifoo = new Foo();
ifoo.M();

the compiler generates code that says "ask the object what method is in the slot for IFoo.M, and call that method.

If an interface is a collection of slots that contain methods, then some of those slots can also contain the get and set methods of a property, the get and set methods of an indexer, and the add and remove methods of an event. But a field is not a method. There's no "slot" associated with a field that you can then "fill in" with a reference to the field location. And therefore, interfaces can define methods, properties, indexers and events, but not fields.

Why can't variables or fields be declared in interface + c#

Lets say that it can be defined. So:

interface Foo
{
int Number;
string Text;
}

class Bar : Foo
{
public int Number;
public string Text;
}

So, in each derived class (class that implements Foo interface) you would have to create two public members. That, at least to me, makes no sense.

If you want your classes to have some members that are not methods, and you would like to simplify it as much as possible, take a look at Auto-Implemented Properties.

Interface: Why can i use a property but not a field?

Interfaces consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members. Interfaces members are automatically public, and they cannot include any access modifiers.

An interface has the following properties:

An interface is like an abstract base class: any non-abstract type that implements the interface must implement all its members.

An interface cannot be instantiated directly.

Interfaces can contain events, indexers, methods, and properties.

Interfaces contain no implementation of methods.

Classes and structs can implement more than one interface.

An interface itself can inherit from multiple interfaces.

Interfaces cannot contain fields

I'm clearly trying to define a property, which as far as I am aware is fine in an interface.

There's nothing wrong with this code. The error you are getting is somewhere else. Just make sure that the AttributeTypeCode class is defined of course.

Can an Interface contain a variable?

No. An interface cannot contain a field.

An interface can declare a Property, but it doesn't provide any implementation of it, so there's no backing field. It's only when a class implements an interface that a backing field (or automatic property) is needed.

Fields in interfaces

All fields in interface are public static final, i.e. they are constants.

It is generally recommended to avoid such interfaces, but sometimes you can find an interface that has no methods and is used only to contain list of constant values.

How do you add an Actionstring to an interface?

You'd need to add it as a property:

public interface IYourInterface
{
Action<string> YourAction { get; set; }
}

Without the get/set it's just a field, and as the compiler points out interfaces can't contain fields. This does mean that when you implement this interface you'll need to supply the actual property as well (though obviously it can be a simple auto-property):

public class Foo : IYourInterface
{
public Action<string> YourAction { get; set; }

// ...
}

Given that, you can then use your Action<string> from the interface:

IYourInterface iFoo = new Foo();

iFoo.YourAction = s => Console.WriteLine(s);

iFoo.YourAction("Hello World!");

As Hans indicated, you can indicate in your interface just a get (or even just a set) if you want. This doesn't mean the class can't have the other, it just means it won't be accessible through the interface. For example:

public interface IYourInterface
{
Action<string> YourAction { get; }
}

public class Foo : IYourInterface
{
public Action<string> YourAction { get; set; }
}

So in the above code, you could access the YourAction property only as a get through the interface, but you could set or get it from the Foo class.



Related Topics



Leave a reply



Submit