Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible to initialize an interface?

Tags:

c#

.net

interface

What I have learned so far is that we can not create an instance of an interface.

    interface IFood
    {
        string Color { get; set; }
    }
    class Apple : IFood
    {
        public string Color { get ; set; }
    }

IFood food = new IFood(); //this gives compile error

IFood food = new Apple(); //this will work

Upto here everything were okay. But when I work with Microsoft.Office.Interop.Excel I have seen something like below

Application excel = new Application();// Application is an interface

What am I missing here?

Meta data of Application interface

like image 663
Beingnin Avatar asked Dec 31 '19 05:12

Beingnin


People also ask

Can you initialize an interface?

You do not 'initialize' an interface. If you create a class that 'implements' an interface, you are creating a class that gives an implementation for every abstract method that is in that interface. No doubt other people will point to abstract classes, but that for some other time.

Can we initialize variable in interface?

No you can not declare variable in interface. No, we can't declare variables, constructors, properties, and methods in the interface.

Is it possible to initialize a variable present in an interface in Java?

Interface variables are static because java interfaces cannot be instantiated on their own. The value of the variable must be assigned in a static context in which no instance exists. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned.

Can you initialize an interface in C#?

Interfaces can't be instantiated by definition. You always instantiate a concrete class. So in both statements your instance is actually of type UnityContainer .


3 Answers

It was an interop ability of COM

Microsoft.Office.Excel API including the Application class, are written in C++

Due to architectural in C++ are more freedom, initialize an interface is needed in some case

.

.NET uses CoClass attribute on a COM object to workaround with initiate an interface

C# wont allow to initiate an interface, but with a CoClass attribute, the interface initialization can be routed to the class CoClass

(example code worth thousand words) So lets reproduce this workaround:

[CoClass(typeof(SugarGlider))] 
[ComImport] // pretend as a COM class
[Guid("000208D5-0000-0000-C000-000000000046")] // put it randomly just to fool the ComImport
public interface ISquirrel
{
     string Foo();
}

[ClassInterface(ClassInterfaceType.None)]
public class SugarGlider : ISquirrel
{
    public string Foo(){ return "Bar"; }
}

You can now initiate the interface by new ISquirrel()

Full example and runs online: https://rextester.com/ORAZQ51751

like image 67
nyconing Avatar answered Nov 07 '22 20:11

nyconing


It uses CoClass attribute, which is a COM concept. That attribute allows you tell the compiler that your interface will be implemented by Application class, thus allows you to instantiate the interface like that.

like image 44
JohanP Avatar answered Nov 07 '22 20:11

JohanP


The magic is happening because of the CoClass attribute. It declares that the interface Application is to be implemented by ApplicationClass

That’s why the compiler allows Application excel = new Application(); since it can infer what class to instantiate (i.e. ApplicationClass)

What does the C# CoClass attribute do?

How does the C# compiler detect COM types?

like image 24
NGambit Avatar answered Nov 07 '22 21:11

NGambit