News Features of C#: Optional and Named Parameters in Terms of COM Interoperability

News Features of C#: Optional and Named Parameters in Terms of COM Interoperability


To detail the concepts of C# new features that we enlisted on our first post, we explained in depth about the Dynamic Lookup and its usages. In this current post, let us focus on the advantages of named and optional parameters and COM interoperability in terms of named and optional parameters.

When we speak of COM interoperability, it is obviously known to all of us that the crucial role played by the .NET Common Language Runtime environment enables the COM objects to intercommunicate with each other by making them think that it is communicating with an object that belongs to its own architecture. A wrapper is actually provided known as Runtime Callable Wrapper (RCW) to make a .NET client application that it is actually speaking with a .NET object. Similarly when a COM client communicates with a .NET object, COM Callable Wrapper (CCW) actually creates an illusion to the client that it is actually communicating with .NET object.

Named and Optional Parameters:

Though we had already provided an example snippet on how to use and invoke the named and optional parameters, there are yet few important concepts behind these two techniques and let us go through them in detail.

People often seem to confuse themselves that both named and optional parameters are one and the same. Actually, they provide a enhanced facility while calling COM interfaces such as Microsoft Office Automation APIs.  A good example for this AutoFormat method of Microsoft Excel Range interface has seven optional parameters.

While optional parameters makes us to omit certain arguments if need be while making calls, named parameters associate an argument by its name rather than by its position. Due to this ability of named parameters, one need not remember arguments’ position or look up the order of the arguments. With the help of optional parameters, one can set default values for all of the arguments so that any one can be omitted and set to its default value without causing any runtime problems upon omission. Nevertheless, both these techniques can be applied in methods, indexers, constructors, and delegates.

A notable point is that optional parameters should always occur at the end of the list and default values supplied to them should be a constant. Optional parameters can also be declared using .NET OptionalAttribute class and while using the OptionalAttribute parameters, we need not specify default value.

How does COM Interoperability works with Optional & Named Parameters?

Before C# 4.0 came up with this feature, F# language had already this feature implemented. The concept being the same in both these languages, the implementation technique alone differs that any developer can learn who understands thoroughly the concept. Thus, when a developer wants to use a COM object developed using either of the language can easily avail the .NET interoperability in each other.

Optional & Named Parameters: Rules of Thumb

Optional & named parameters cannot be equated to the so-called elderly overloading concept of the object oriented programming model.

  1. If more than one method is defined of the same name but one with optional parameters and other with the exact parameter type that the call matches, then preference is given to the latter by the method that is invoked with that exact parameter.
  2. If no method of that signature is available of the exact type parameter, then preference is given to the method in which the type of the invoking method is implicitly convertible.
  3. If all the methods are equally acceptable, then preference is given to one that does not belong to optional parameter category.

With these set of rules on our thumb, we can easily structure our code that would not conflict with any of the existing API while making COM calls.

, , , , , , , , ,

Open chat
Chat with our Experts!
Can I help you?