Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating instance of interface in C#

Tags:

c#

oop

I'm working with MS Excel interop in C# and I don't understand how this particular line of code works:

var excel = new Microsoft.Office.Interop.Excel.Application();

where Microsoft.Office.Interop.Excel.Application is an INTERFACE defined as:

[Guid("000208D5-0000-0000-C000-000000000046")]
[CoClass(typeof(ApplicationClass))]
public interface Application : _Application, AppEvents_Event
{
}

I'm thinking that some magic happens when the interface is decorated with a CoClass attribute, but still how is it possible that we can create an instance of an interface with a new keyword? Shouldn't it generate a compile time error?

like image 477
Max Avatar asked Feb 27 '23 08:02

Max


1 Answers

Ayende blogged about this.

like image 172
Darin Dimitrov Avatar answered Mar 10 '23 07:03

Darin Dimitrov