Programming with Visual Basic vs. C# in Office Solutions

You can create Visual Studio Tools for Office solutions by using either Visual Basic or C#. Because the Microsoft Office object models were designed to be used with Microsoft Visual Basic for Applications (VBA), Visual Basic developers can work comfortably with the objects exposed by the Microsoft Office applications. Developers who use C# do not have the same shortcuts when they work with the Microsoft Office object model. There are also some differences between basic programming features in Office development and managed code written in Visual Basic and C#.

Key Differences Between Visual Basic and C#

The following table shows key differences between Visual Basic and C# in Office development.

Feature

Description

Visual Basic support

C# support

Optional parameters

Many Microsoft Office methods have parameters that are not required when you call the method. If no value is passed for the parameter, a default value is used.

Visual Basic supports optional parameters.

C# does not support optional parameters. Instead, you must pass a value for all parameters. For more information, see The Variable missing and Optional Parameters in Office Solutions.

Passing parameters by reference

Optional parameters in most of the Microsoft Office primary interop assemblies can be passed by value. However, in some primary interop assemblies, optional parameters that accept reference types must be passed by reference.

For more information about value and reference type parameters, see Passing Arguments by Value and by Reference (for Visual Basic) and Passing Parameters (C# Programming Guide).

No additional work is needed to pass parameters by reference. The Visual Basic compiler automatically passes the parameters by reference when necessary.

Optional parameters that accept reference types in some primary interop assemblies (such as the primary interop assembly for Word) must be passed using the ref keyword, and you must pass a variable that contains the actual value.

Parameterized properties

Some properties accept parameters, and act as read-only functions.

Visual Basic supports properties that accept parameters.

C# requires using the get and set accessors for setting and retrieving property values that accept parameters.

Late binding

Late binding involves determining the properties of objects at run time, instead of casting variables to the object type at design time.

Visual Basic can perform late binding when the Option Strict statement is set to Off. For more information, see Option Strict Statement.

C# does not perform late binding directly. However, you can use types in the System.Reflection namespace to determine available members of a specified type and perform a type of late binding.

Key Differences Between Office Development and Managed Code

The following table shows key differences between Office development and managed code written in Visual Basic or C#.

Feature

Description

Visual Basic and C# support

Array indexes

The lower array bounds of collections in Microsoft Office applications begins with 1. Visual Basic and Visual C# use 0-based arrays. For more information, see Arrays (C# Programming Guide) and Overview of Arrays in Visual Basic.

To access the first item of a collection in the object model of a Microsoft Office application, use the index 1 instead of 0.

See Also

Tasks

How to: Target Office Applications Through Primary Interop Assemblies

How to: Cast Objects Returned by Office Applications

Concepts

Writing Code in Office Solutions

The Variable missing and Optional Parameters in Office Solutions

Change History

Date

History

Reason

August 2008

Added information about array indexes.

Information enhancement.