Dela via


Programming with Visual Basic vs. Visual C# in Office Solutions

You can create Office solutions by using either Visual Basic or Visual 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. In Visual Studio 2010, Visual C# developers can use most of the same features as Visual Basic developers, but there are some cases where they must write additional code to use the Office object models. There are also some differences between basic programming features in Office development and managed code written in Visual Basic and C#.

Applies to: The information in this topic applies to document-level projects and application-level projects for Microsoft Office 2010 and the 2007 Microsoft Office system. For more information, see Features Available by Office Application and Project Type.

Key Differences Between Visual Basic and Visual C#

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

Feature

Description

Visual Basic support

Visual 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.

Visual C# supports optional parameters in most cases. For more information, see 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 (Visual Basic) (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.

In most cases, the Visual C# compiler automatically passes the parameters by reference when necessary. For more information, see Optional Parameters in Office Solutions.

Parameterized properties

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

Visual Basic supports properties that accept parameters.

Visual C# supports properties 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 performs late binding when Option Strict is off. When Option Strict is on, you must explicitly convert objects and use types in the System.Reflection namespace to access late-bound members. For more information, see Late Binding in Office Solutions.

Visual C# performs late binding in projects that target the .NET Framework 4. In projects that target the .NET Framework 3.5, you must explicitly cast objects and use types in the System.Reflection namespace to access late-bound members. For more information, see Late Binding in Office Solutions.

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 Visual C#.

Feature

Description

Visual Basic and Visual 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 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

Reference

Reflection (C# and Visual Basic)

Concepts

Writing Code in Office Solutions

Late Binding in Office Solutions

Optional Parameters in Office Solutions