---
title: "Writing a Greeter Client"
url: https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client
language: csharp
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Writing a Greeter Client

Programming language: C#. Other languages: [C++](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/cpp.md), [Java](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/php.md), [Python](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/swift.md).

This page presents a step-by-step guide to writing the client-side of our C# Greeter application.

This client creates a proxy to a remote object that implements the `Greeter` interface and invokes the `greet` operation on this object.

You can find the complete source code for this example in the [ice-demos repository](https://github.com/zeroc-ice/ice-demos/tree/3.8/csharp/Ice/Greeter).

## Compile Slice File with Slice Compiler

The first step when writing a C# application with Ice is to compile the Slice definitions for this application with the Slice to C# compiler (`slice2cs`).

To compile `Greeter.ice`, the demo adds the `ZeroC.Ice.Slice.Tools` NuGet package to the project and `Greeter.ice` as a `SliceCompile` item; MSBuild then runs `slice2cs` as part of every build. By default, the package adds every `.ice` file under the project directory as a `SliceCompile` item, but the demo keeps `Greeter.ice` in `../slice`, outside the project directory, so its project file adds it explicitly:

```xml title="Client.csproj"
<ItemGroup>
  <SliceCompile Include="../slice/Greeter.ice" />
  <PackageReference Include="ZeroC.Ice" Version="3.8.*" />
  <PackageReference Include="ZeroC.Ice.Slice.Tools" Version="3.8.*" PrivateAssets="all" />
</ItemGroup>
```

See [Slice Tools for Ice and C#](https://github.com/zeroc-ice/ice/blob/3.8/csharp/src/ZeroC.Ice.Slice.Tools/README.md) for the `SliceCompile` item metadata, and [Using the Slice Compiler](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/csharp.md) for the options of `slice2cs`.

The build generates a single file named `Greeter.cs`, which provides the APIs that our client code calls.

## Client Implementation

Since all the code generated by `slice2cs` was generated into the `VisitorCenter` namespace, we start with a using directive to let us reference it without qualification:

```csharp
using VisitorCenter;
```

Then the rest of the client logic can be broken down into four pieces:

### 1. Create a Communicator

We create a [Communicator](https://code.zeroc.com/ice/3.8/api/csharp/api/Ice.Communicator.html) using its constructor:

```csharp
await using var communicator = new Ice.Communicator(ref args);
```

The communicator is the main entry point into the Ice runtime. Its responsibilities include establishing connections to servers, caching these connections, and managing configuration properties. We also need a communicator to create a proxy (see next step).

Our client, like most Ice applications, creates a single communicator.

It is important to make sure that your communicator is properly disposed when no longer needed. This ensures that network connections are gracefully closed, threads are joined, and other important clean-up occurs. The easiest way to do this is with an `await using` like we do here.

### 2. Create a Greeter Proxy

Next, we need a way to call on a remote `Greeter` object. In Ice, this is done with *proxies*. Proxies are local constructs that represent remote Ice objects and provide methods to call operations on those objects.

We create a `Greeter` proxy by calling `createProxy` on the `GreeterPrxHelper` class generated by the Slice compiler. This returns a new instance of `GreeterPrx` (a `Greeter` proxy):

```csharp
GreeterPrx greeter = GreeterPrxHelper.createProxy(
    communicator,
    "greeter:tcp -h localhost -p 4061");
```

`createProxy` accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says that the target Ice object is named “greeter” and can be reached via `tcp` on `localhost` on port `4061`.

> **Note**
>
> The name of the interface (`Greeter`) and the identity of the Ice object (`greeter`) are independent. The Ice objects hosted in the server could just as easily have identities like `santa`, `bugsBunny`, etc.

### 3. Make an Invocation

The third step is to call `greet` on the remote Ice object using our proxy and to print the greeting:

```csharp
string greeting = await greeter.GreetAsync(Environment.UserName);
Console.WriteLine(greeting);
```

The `GreetAsync` method does all the heavy lifting for us: the proxy creates a request with the username string, the communicator establishes a connection to `localhost:4061`, and the request is sent over it. When a response is received, the proxy will unmarshal its payload and finally return a string (the greeting).

Using `async`/`await` for this invocation offers a couple advantages:

- The calling thread can continue doing other work while `GreetAsync` waits for I/O.
- The `await` keyword signals to the reader that `GreetAsync` is a remote call that may take time to complete.

### 4. Cleanup

Finally, at the end of our logic, our communicator goes out of scope and is disposed (because we used `await using`), and then our application exits.

## Running the Client

After building the client (see the demo’s [README](https://github.com/zeroc-ice/ice-demos/blob/3.8/csharp/Ice/Greeter/README.md) for instructions), you can run it with `dotnet`:

```shell
cd Client
dotnet run
```

> **Note**
>
> This client won’t work unless you’ve also launched a Greeter server — see [Writing a Greeter Server](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-server/csharp.md).
