In C#, the interface can be instantiated?

前端 未结 7 1910
孤街浪徒
孤街浪徒 2021-01-20 21:29

I\'m reading the code in here. I find that private ITreeModel _model; in TreeList.cs:

namespace Aga.Controls.Tree
{
    public class TreeList: L         


        
相关标签:
7条回答
  • 2021-01-20 21:38

    It's implemented somewhere else in the code. If you call _model.GetType().ToString() you will see it is not just an interface.

    But to answer your question correctly, YES, an interface can be instantiated. Some of you may think "no it can't", but it can be done (with some COM hacks):

    class Foo : IFoo
    {
        readonly string name;
        public Foo(string name)
        {
            this.name = name;
        }
        string IFoo.Message
        {
            get
            {
                return "Hello from " + name;
            }
        }
    }
    // these attributes make it work
    // (the guid is purely random)
    [ComImport, CoClass(typeof(Foo))]
    [Guid("d60908eb-fd5a-4d3c-9392-8646fcd1edce")]
    interface IFoo
    {
        string Message {get;}
    }
    //and then somewhere else:
    IFoo foo = new IFoo(); //no errors!
    

    Here is my source.

    0 讨论(0)
  • 2021-01-20 21:39

    Of course you can do this, but underlying object must implement this Interface. So you can do something like

    ITreeModel _model  = new TreeModel();
    

    Where

    public class TreeModel:ITreeModel
    {
      ...
    }
    
    0 讨论(0)
  • 2021-01-20 21:40

    From Interfaces (C# Programming Guide)

    An interface can't be instantiated directly. Its members are implemented by any class or struct that implements the interface.

    0 讨论(0)
  • 2021-01-20 21:44

    You can never instantiate an interface in C# directly, but yes you can instantiate a subclass implementing that interface. For example:

    interface IShape
    {
       //Method Signature
       void area(int r);
    }
    
    public class Circle : IShape
    {
       //method Implementation
       void area(int r)
       {
          float area;
          area = 3.14 * r * r;
          Console.WriteLine("The area of the circle is: {0}",area);
       }
    }
    
    public class Shapes
    {
       public static void Main() {
          //Uncommenting the following line will cause compiler error as the  
          // line tries to create an instance of interface.
          // interface i = new IShape();
    
         // We can have references of interface type.
         IShape i = new Circle();
         i.area(10);
       }
    }
    
    0 讨论(0)
  • 2021-01-20 21:54

    _model is a member of TreeList and that means that you can create an instance of a class and then it will contain an instance of some class. for example

    _model = new TreeModel(); 
    

    will make _model contain an instance

    but you cannot do

    _model = new ITreeModel(); 
    

    because ITreeModel is and interface and you cannot create an instance of an interface

    0 讨论(0)
  • 2021-01-20 21:56

    That _model should contain an instance of a class that implements that ITreeModel interface (or it's null).

    0 讨论(0)
提交回复
热议问题