Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interfaces cannot contain fields

Tags:

c#

interface

probably a really dumb question, but I keep getting the above error with the following code:

public interface IAttributeOption
{
    AttributeTypeCode Type { get; set; }
}

You can probably tell, I'm trying to define an interface with a single property.

I know that somebody is BOUND to tell me "an interface is a virtual class, blah blah" and I want to confirm in advance I KNOW THIS! I'm clearly trying to define a property, which as far as I am aware is fine in an interface.

So what is wrong??

Thanks :)

like image 327
CompanyDroneFromSector7G Avatar asked Sep 25 '12 11:09

CompanyDroneFromSector7G


People also ask

CAN interfaces contain fields?

An interface can't contain instance fields, instance constructors, or finalizers. Interface members are public by default, and you can explicitly specify accessibility modifiers, such as public , protected , internal , private , protected internal , or private protected .

Why interface Cannot contain fields?

Interface cannot contain fields because they represent a particular implementation of data. Multiple inheritance is possible with the help of Interfaces but not with classes.

Which Cannot be included in interface?

An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. An interface is not extended by a class; it is implemented by a class. An interface can extend multiple interfaces.

Can Java interfaces have fields?

A Java interface is a bit like a Java class, except a Java interface can only contain method signatures and fields. A Java interface is not intended to contain implementations of the methods, only the signature (name, parameters and exceptions) of the method.


1 Answers

So what is wrong?

Nothing, your interface declaration is fine. Sounds like you possibly forgot to put your accessor declarations in previously:

public interface IAttributeOption
{
    AttributeType Type; // no { get; set; }
}

If it's a linked assembly you may need to do a full rebuild as you may be pulling in a cached version.

like image 178
James Avatar answered Oct 12 '22 21:10

James