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.Appli
Ayende blogged about this.
Actually code that you mentioned created instance of the ApplicationClass
class and that is what CoClass
attribute does.
See this answer for details: How does the C# compiler detect COM types?
ApplicationClass is implement Application interface. In two words, interface is declaration of methods of class. Your line of code create instance of class ApplicationClass (because interface have attribute with class with constructor), query this instance of interface Application and put this to variable excel.
On second question: no, you can't create interface with 'new' keyword. Because, any interface have only declaration of methods, not implementation. You can try this for creating you own classes and interfaces:
interface MyIntf {
void method1(string s1);
}
public class MyIntfImplementation : MyIntf {
void method1(string s1) {
// do it something
}
}
After this you can use this:
MyIntf q = new MyIntfImplementation();
q.method1();