Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

The VISA I/O API & .NET


November, 2004: The VISA I/O API & .NET

Variable Argument Lists in .NET and Unmanaged Interoperability

Different programming technologies have struggled with providing powerful but easy-to-use formatted I/O. I/O formatting is turning various computer data types into streams of bytes that can be sent across the wire, such as across a serial port or across a TCPIP socket. It is inconvenient, tedious, and error prone to have a separate method call for each data element in a serial data transfer, so programming elements such as C's variable argument lists (like in C's printf function) were invented.

.NET has a different mechanism for variable argument lists—the params keyword. In the .NET intermediate language (IL, the assembly language of .NET), the parameters are turned into an array of value object references, and primitives like integers are "boxed" into object wrappers placed in the array.

Microsoft chose not to allow C-style variable argument lists through the params keyword for a number of reasons. The params keyword is not a good fit for C-style variable argument lists, it does not support out or ref passing of parameters, but C-style variable argument lists allow the values pointed to by the pointer arguments to change. Another reason why C-style variable argument lists are a bad fit for .NET is that because variable argument lists are type checked at runtime, there is a large risk of memory-access violations undetectable by the compiler that defeat the memory safety Microsoft tried to provide with the .NET programming technology.

That said, Microsoft did leave in some hidden keywords that allow C-style variable argument lists in .NET—the __arglist and __makeref keywords. A method declared with __arglist accepts a C-style variable argument list, and by wrapping a comma-separated list of variables in an __arglist() declaration, a variable-length argument list can be passed to such methods. If methods using __arglist are decorated with the DllImportAttribute, they can be used for calling such unmanaged DLL functions as printf or scanf. Reference parameters are passed by wrapping the variable name in a makeref() keyword, which is the equivalent of the C practice of putting an ampersand (&) in front of the variable to pass it its memory address rather than its value to the function being called.

These keywords were not used in the VISA header files in .NET for several reasons: The risks of memory access violations and the added burden of forcing you to understand the appropriate times to use keywords such as __arglist and __makeref were good reasons not to go down this route. Additionally, performance is a consideration. It has been reported that the performance of these hidden keywords is about five times worse than params, which makes it many times slower than a standard method call.

—D.G.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.