using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
interface IInterface
{
}
public interface Icar
{
int doors;
string name;
bool suv
}
class Car : Icar
{
public int doors;
public string name;
public bool suv;
}
class Program
{
static void Main(string[] args)
{
Car s = new Car { doors = 5, name = "my name", suv = true };
Car t = new Car { doors = 2, name = "my name again", suv = false };
Console.WriteLine("test");
Console.ReadLine();
}
}
}
Asked
Active
Viewed 3,675 times
1

Hello-World
- 9,277
- 23
- 88
- 154
-
1Just as the error says, an Interface can not contain `fields`, it can contain `Properties`, so for example, in `Icar` change `int doors`, to `int Doors {get;}` – Ryan Wilson Jan 17 '19 at 18:51
-
2The error is *very* clear. Interfaces can only contain properties or methods, not fields. – Frontear Jan 17 '19 at 18:52
-
@User, well found! :) – Rob Jan 17 '19 at 19:02
-
1@Hello-World, The accepted answer on the question yours has been closed as a duplicate of was written by Eric Lippert - (you may already be aware of this but,..) he previously worked on the C# compiler at Microsoft, so he **knows his stuff**, his answer is definitely worth a read :) Oddly enough, the question also involves an `ICar` interface! – Rob Jan 17 '19 at 19:03
2 Answers
5
Because you haven't set your fields as actual properties. Interfaces do not support fields; as the error message states.
Simply change
public int doors;
public string name;
public bool suv;
To
int doors {get; set;}
string name {get; set;}
bool suv {get; set;}

Darren Wainwright
- 30,247
- 21
- 76
- 127
-
@RyanWilson - you're right; lazy paste on my part. `set` is accepted however. – Darren Wainwright Jan 17 '19 at 18:55
-
Yeah, my bad on the `set` I tested against a class which was `immutable`. – Ryan Wilson Jan 17 '19 at 18:56
-
-
@RyanWilson - no worries, wasn't concerned. Little odd, given the answer is correct - hey ho, that's SO for you :) – Darren Wainwright Jan 17 '19 at 18:57
-
Yeah, it would be nice if they had to at least give a comment on a way to improve your answer to satisfy their critique. Oh well, since you fixed your answer, I up-voted. At least you're breaking even now. :) – Ryan Wilson Jan 17 '19 at 18:58
1
Because it's not permitted. As per the C# reference for interfaces:
An interface can be a member of a namespace or a class and can contain signatures of the following members:
- Methods
- Properties
- Indexers
- Events
The C# Programming guide goes into more detail, calling out the things that can't be included in an interface:
An interface can't contain constants, fields, operators, instance constructors, finalizers, or types. Interface members are automatically public, and they can't include any access modifiers. Members also can't be static.