-1
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();
        }
    }
}
Hello-World
  • 8,457
  • 22
  • 78
  • 151
  • Just 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
  • 2
    The 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 Answers2

1

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
  • 29,369
  • 20
  • 72
  • 123
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.

Community
  • 1
  • 1
Rob
  • 44,468
  • 23
  • 118
  • 147