Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, the interface can be instantiated?

Tags:

c#

interface

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

namespace Aga.Controls.Tree
{
    public class TreeList: ListView
    {
        #region Properties
        //...
        private ITreeModel _model;
        public ITreeModel Model
        {
            //...
        }
        //...
    }
}

and the ITreeModel is a interface in ITreeModel.cs:

namespace Aga.Controls.Tree
{
    public interface ITreeModel
    {
        /// <summary>
        /// Get list of children of the specified parent
        /// </summary>
        IEnumerable GetChildren(object parent);

        /// <summary>
        /// returns wheather specified parent has any children or not.
        /// </summary>
        bool HasChildren(object parent);
    }
}

the _model is a instantiated object?

Edited:

TreeList.cs:

namespace Aga.Controls.Tree
{
    public class TreeList: ListView
    {
        #region Properties

        /// <summary>
        /// Internal collection of rows representing visible nodes, actually displayed in the ListView
        /// </summary>
        internal ObservableCollectionAdv<TreeNode> Rows
        {
            get;
            private set;
        } 

        private ITreeModel _model;
        public ITreeModel Model
        {
            get { return _model; }
            set 
            {
                if (_model != value)
                {
                    _model = value;
                    _root.Children.Clear();
                    Rows.Clear();
                    CreateChildrenNodes(_root);
                }
            }
        }

        private TreeNode _root;
        internal TreeNode Root
        {
            get { return _root; }
        }
        //....
    }
}

}

Edited2:

Somewhere:

public partial class RegistrySample : UserControl
{
    public RegistrySample()
    {
        InitializeComponent();
        _tree.Model = new RegistryModel();
    }
}

class RegistryModel : ITreeModel

like image 269
SubmarineX Avatar asked Oct 22 '25 05:10

SubmarineX


2 Answers

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
{
  ...
}
like image 189
Kamil Budziewski Avatar answered Oct 23 '25 18:10

Kamil Budziewski


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);
   }
}
like image 32
Tahir77667 Avatar answered Oct 23 '25 20:10

Tahir77667