# C# quick start

At the end of this guide, you will have created a simple . NET console **`Hello,
World!`** program that connects to the Memgraph database and executes simple
queries.

## Prerequisites

If you're new to Memgraph or you're in a developing stage, we
recommend using the Memgraph Platform. Besides the database, it also
includes all the tools you might need to analyze your data, such as command-line
interface [mgconsole](https://memgraph.com/docs/getting-started/cli), web interface [Memgraph
Lab](https://memgraph.com/docs/data-visualization) and a complete set of algorithms within a
[MAGE](https://memgraph.com/docs/advanced-algorithms) library.

Ensure [Docker](https://docs.docker.com/engine/install/) is running in the
background. Depending on your operating system, execute the appropriate command
in the console:

**For Linux and macOS:**

```bash
curl -sSf "https://install.memgraph.com" | sh
```

**For Windows:**

```bash
iwr https://install.memgraph.com/windows -useb | iex
```

The command above will start [Memgraph
Platform](https://memgraph.com/docs/getting-started/install-memgraph/docker#install-memgraph-platform),
which includes [Memgraph
database](https://memgraph.com/docs/getting-started/install-memgraph/docker#other-available-docker-images),
[Memgraph Lab](https://memgraph.com/docs/data-visualization#quick-start) and [Memgraph
MAGE](https://memgraph.com/docs/advanced-algorithms#quick-start). Memgraph uses Bolt protocol to
communicate with the client using the exposed 7687 port. Memgraph Lab is a web
application you can use to visualize the data. It's accessible at
[http://localhost:3000](http://localhost:3000) if Memgraph Platform is running
correctly. The 7444 port enables Memgraph Lab to access and preview the logs,
which is why both of these ports need to be exposed. 

For more information visit the getting started guide on [how to run Memgraph
with Docker](https://memgraph.com/docs/getting-started/install-memgraph/docker).

> **Warning**
>
> **For Memgraph < 2.11**, in order for the Neo4j driver to work, you need [modify configuration
> setting](https://memgraph.com/docs/database-management/configuration)
> `--bolt-server-name-for-init`. When running Memgraph, set
> `--bolt-server-name-for-init=Neo4j/5.2.0`. If you use other version of Neo4j
> driver, make sure to put the appropriate version number.

## Driver

Please note that the code samples in this guide utilize the
`Neo4j.Driver.Simple` package which implements a blocking interface around the
'main' driver. It should be used as a tool for getting started quickly. The
`Neo4j.Driver` package contains the official and complete driver for real-world
projects. The driver documentation can be found here: [Neo4j . NET
Driver](https://github.com/neo4j/neo4j-dotnet-driver).

The [Blueprint41](https://github.com/circles-arrows/blueprint41), .NET ORM, also
supports Memgraph. Blueprint 41 supports the definition of the object model as
a schema. It offers refactoring scripts written in CSharp, which you can add to
your project. When you run your program with an older graph version, the
upgrade script will automatically be executed against the graph. It also
supports the generation of type-safe data objects, so you know at compile time
if your code is compatible with the latest upgrades.

## Basic Setup

We'll be using Visual Studio 2022 on Windows 10 to connect a simple . NET
console application to a running Memgraph instance. If you're using a different
IDE, the steps might be slightly different, but the code is either the same or
very similar.

Let's jump in and connect a simple program to Memgraph.

**1.** Open **Visual Studio** and create a new project.
 **2.** Find and
select the **Console App (. NET Core)** template by using the search box.

**3.** Name your project **_MemgraphApp_**, choose an appropriate location for
it, and click **Create**.
 **4.** Select the **Tools > Manage NuGet
Packages** menu command.
 **5.** Once the window opens, search for the
**Neo4j.Driver.Simple**.
 **6.** Select the appropriate driver and click **Add
package**.

Now, you should have the newest version of the driver installed and can proceed
to copy the following code into the **Program.cs** file.

```csharp
using Neo4j.Driver;

namespace MemgraphApp
{
    public class Program
    {
        public static void Main()
        {
            string message = "Hello, World!";

            using var _driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.None);
            using var session = _driver.Session();

            var greeting = session.ExecuteWrite(tx =>
            {
                var result = tx.Run("CREATE (n:FirstNode) " +
                                    "SET n.message = $message " +
                                    "RETURN 'Node '  + id(n) + ': ' + n.message",
                    new { message });
                return result.Single()[0].As<string>();
            });
            Console.WriteLine(greeting);
        }
    }
}
```

Once you run the program, you should see an output similar to the following:

```
Node 1: Hello, World!
```

> **Warning**
>
> To configure indexes and constraints properly, do one operation at a time and
>   use the non-transactional API: ```cs await session.RunAsync(query: "CREATE
>   INDEX ON :FirstNode"); await session.RunAsync(query: "CREATE INDEX ON
>   :FirstNode(message)"); ```

## Alternative Setup

If you want to try out more complex operations, feel free to use the refactored
code below.

```csharp
using Neo4j.Driver;

namespace MemgraphApp
{
    public class Program : IDisposable
    {
        private readonly IDriver _driver;

        public Program(string uri, string user, string password)
        {
            _driver = GraphDatabase.Driver(uri, AuthTokens.Basic(user, password));
        }

        public void PrintGreeting(string message)
        {
            using (var session = _driver.Session())
            {
                var greeting = session.ExecuteWrite(tx =>
                {
                    var result = tx.Run("CREATE (n:FirstNode) " +
                                        "SET n.message = $message " +
                                        "RETURN 'Node '  + id(n) + ': ' + n.message",
                        new { message });
                    return result.Single()[0].As<string>();
                });
                Console.WriteLine(greeting);
            }
        }

        public void Dispose()
        {
            _driver?.Dispose();
        }

        public static void Main()
        {
            using (var greeter = new Program("bolt://localhost:7687", "", ""))
            {
                greeter.PrintGreeting("Hello, World!");
            }
        }
    }
}
```

> **Info**
>
> If you encounter serialization errors while using C# client, we recommend
> referring to our [Serialization errors](https://memgraph.com/docs/help-center/errors/serialization) page
> for detailed guidance on troubleshooting and best practices.
