# C# (Visual Studio)

## Option 1: NuGet Package Manager Console&#x20;

Open the NuGet Package Manager Console: *Tools -> NuGet Package Manager -> Package Manager Console* and  write the following command:

```sh
NuGet\Install-Package Nep
NuGet\Install-Package Newtonsoft.Json
NuGet\Install-Package NetMQ
```

## Option 2: NuGet Package Manager for Solution Interface&#x20;

Open the NuGet Package Manager for Solution Interface: *Tools -> NuGet Package Manager ->  Manage NuGet Package for Solution* and browse **N*****ep**.* Select the projects where the NEP library for C# will be installed and select the install button. In the same way, install the ***Newtonsoft.Json*** and **NetMQ** packages

## Test a basic Publisher

```csharp
using Nep;

namespace NepTest
{
    // Message to send, used for serialization in JSON
    class Msg
    {
        public int message { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Create a new nep node
            // The parameter given to this function must be different for each script
            NepNode node = new NepNode("basic_pub_cs");
            // Create a new publisher with the topic "test"
            NepPublisher pub = node.NewPub("test", "json");
            // Define the message to send 
            Msg msg = new Msg();
            int i = 0;
            while (i < 50)
            {
                i++;
                // Fill message to send
                msg.message = i;
                // Send the message with the next line
                pub.Publish(msg);
                // Use the next line to send messages each 0.5 seconds approx.
                System.Threading.Thread.Sleep(500);
            }
            // Close sockets
            pub.Dispose();
            node.Close();
            Console.WriteLine("Publisher program finished!");
        }
    }
}
```

## Test a basic Subscriber

```csharp
uusing System;
using Nep;
using Newtonsoft.Json;

// Change <NepTest> for the correct namespace
namespace NepTest
{
    // Message to read, used for deserialization
    class Msg
    {
        // JSON key = message, value is an int
        public int message { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Create a new Nep Node with a specified name
            NepNode node = new NepNode("basic_sub_cs");

            // Create a new Subscriber for the "test" topic with "json" message type
            NepSubscriber sub = node.NewSub("test", "json");

            String msg = "";

            // Print the received messages
            int i = 0;
            while (i < 50)
            {
                // Non-blocking listen to receive a message
                bool ok = sub.Listen(out msg);
                if (ok)
                {
                    // Convert JSON string to Msg object using JsonConvert
                    Msg message = JsonConvert.DeserializeObject<Msg>(msg);
                    Console.WriteLine(message.message);
                    i = i + 1;
                }
            }

            // Close the Subscriber and Node
            sub.Dispose();
            node.Close();

            Console.WriteLine("Subscriber program finished!");
        }
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coronadoenrique.gitbook.io/nep+/programming-enviroments/c-visual-studio.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
