On this page:
Basic C# Mapping for Operations
As we saw in the C# mapping for interfaces, for each operation on an interface, the proxy class contains a corresponding member function with the same name. To invoke an operation, you call it via the proxy. For example, here is part of the definitions for our file system:
name operation returns a value of type
string. Given a proxy to an object of type
Node, the client can invoke the operation as follows:
This illustrates the typical pattern for receiving return values: return values are returned by reference for complex types, and by value for simple types (such as
idempotent Operations in C#
You can add an
idempotent qualifier to a Slice operation. As far as the signature for the corresponding proxy method is concerned,
idempotent has no effect. For example, consider the following interface:
The proxy interface for this is:
idempotent affects an aspect of call dispatch, not interface, it makes sense for the two methods to be mapped the same.
Passing Parameters in C#
In-Parameters in C#
The parameter passing rules for the C# mapping are very simple: parameters are passed either by value (for value types) or by reference (for reference types). Semantically, the two ways of passing parameters are identical: it is guaranteed that the value of a parameter will not be changed by the invocation.
Here is an interface with operations that pass parameters of various types from client to server:
The Slice compiler generates the following proxy for these definitions:
Given a proxy to a
ClientToServer interface, the client code can pass parameters as in the following example:
Out-Parameters in C#
out parameters simply map to C#
Here again are the same Slice definitions we saw earlier, but this time with all parameters being passed in the
The Slice compiler generates the following code for these definitions:
Given a proxy to a
ServerToClient interface, the client code can pass parameters as in the following example:
Null Parameters in C#
Some Slice types naturally have "empty" or "not there" semantics. Specifically, C# sequences, dictionaries, strings, and structures (if mapped to classes) all can be
null, but the corresponding Slice types do not have the concept of a null value.
- Slice sequences, dictionaries, and strings cannot be null, but can be empty. To make life with these types easier, whenever you pass a C#
nullreference as a parameter or return value of type sequence, dictionary, or string, the Ice run time automatically sends an empty sequence, dictionary, or string to the receiver.
- If you pass a C#
nullreference to a Slice structure that is mapped to a C# class as a parameter or return value, the Ice run time automatically sends a structure whose elements are default-initialized. This means that all proxy members are initialized to
null, sequence and dictionary members are initialized to empty collections, strings are initialized to the empty string, and members that have a value type are initialized to their default values.
This behavior is useful as a convenience feature: especially for deeply-nested data types, members that are structures, sequences, dictionaries, or strings automatically arrive as an empty value at the receiving end. This saves you having to explicitly initialize, for example, every string element in a large sequence before sending the sequence in order to avoid
NullReferenceException. Note that using null parameters in this way does not create null semantics for Slice sequences, dictionaries, or strings. As far as the object model is concerned, these do not exist (only empty sequences, dictionaries, and strings do). For example, whether you send a string as
null or as an empty string makes no difference to the receiver: either way, the receiver sees an empty string.
Optional Parameters in C#
The C# mapping for this operation is shown below:
The constructors and conversion operators provided by the
Ice.Optional type simplify the use of optional parameters:
A well-behaved program must not assume that an optional parameter always has a value.
Exception Handling in C#
Slice exceptions are thrown as C# exceptions, so you can simply enclose one or more operation invocations in a
Typically, you will catch only a few exceptions of specific interest around an operation invocation; other exceptions, such as unexpected run-time errors, will typically be handled by exception handlers higher in the hierarchy. For example:
Note that the
ToString method of exceptions prints the name of the exception, any inner exceptions, and the stack trace. Of course, you can be more selective in the way exceptions are displayed. For example,
e.GetType().Name returns the (unscoped) name of an exception.
Exceptions and Out-Parameters in C#
The Ice run time makes no guarantees about the state of out-parameters when an operation throws an exception: the parameter may still have its original value or may have been changed by the operation's implementation in the target object. In other words, for out-parameters, Ice provides the weak exception guarantee  but does not provide the strong exception guarantee.
This is done for reasons of efficiency: providing the strong exception guarantee would require more overhead than can be justified.
- C-Sharp Mapping for Exceptions
- C-Sharp Mapping for Interfaces
- Collocated Invocation and Dispatch
- Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions. Reading, MA: Addison-Wesley. Sutter, H. 1999.