Understanding the Dynamic Keyword in C# 4: Listing 2

Scripting Excel with C#

// Add this line to the beginning of the file:
// using Excel = Microsoft.Office.Interop.Excel;

var excelApp = new Excel.Application();

excelApp.Workbooks.Add();
// excelApp.Workbooks.Add(Type.Missing);

excelApp.Visible = true;

Excel.Range targetRange = excelApp.Range["A1"];
// Excel.Range targetRange = excelApp.get_Range("A1", Type.Missing);

targetRange.Value = "Name";
// targetRange.set_Value(Type.Missing, "Name");

targetRange.Columns[1].AutoFit();
// ((Excel.Range)targetRange.Columns[1, Type.Missing]).AutoFit();

About the Author

Alexandra Rusina is a program manager on the Silverlight team. Prior to that she worked as a programming writer on the Visual Studio Languages team during the Visual Studio 2010 release. She also regularly blogged on the C# Frequently Asked Question blog.