Create Publication

We are looking for publications that demonstrate building dApps or smart contracts!
See the full list of Gitcoin bounties that are eligible for rewards.

Tutorial Thumbnail
Beginner · 30 minutes

Using VS Code with C#

Being able to debug is a great tool when learning new programming content. This tutorial will facilitate how to debug C# using Visual Studio (VS) Code, which can be used with all the code samples on the Community .NET Algorand SDK Repository. Other debuggers can also be used. If you are new to debugging, VS Code is the most popular tool. It supports many programming languages, is quick to load and has a wealth of documentation and extensions.

Requirements

Note: Mono is not supported in VS Code. So, for Xamarin cross-platform Android and iOS C# development – use either Visual Studio for Mac or Visual Studio Community Edition for Windows.

Background

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, macOS and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages. VS Code can take on most of the tasks of an Integrated Development Environment (IDE) with the right configuration and plugin library extension. VS Code is open source.

Extensions are available in most development languages for VS Code. Specific install instuctions are in Step 2 below. For general information see: Java, JavaScript, Go, Python and C#. See extensions for VS Code languages here .

For documentation, several other extensions are available including Grammarly and MarkDown (MD).

This tutorial walks through C# examples.

Steps

1. Download VS Code

Download VS Code from this site

2. Install Extensions

Open VS Code. Go to the Extensions view.
Filter the extensions list by typing your programming language of choice, such as C#, and you will see all related extensions.

Filter Extensions

Figure 2-1 VS Code Extensions for C#

3. Debugging with VS Code

For general information on setting up debugging in VS Code see this:
https://code.visualstudio.com/docs/editor/debugging

Optionally, for Keyboard shortcuts and Keymap extensions, which match other editors, see this:
https://code.visualstudio.com/docs/getstarted/keybindings


Learn More
Highly recommended for new VS Code C# developers - Getting started with VS Code and C# short video

4. Generate Assets for Build and Debug

Create a new empty folder called Demos. Open that folder with VS Code, and click on the. Demos folder in the IDE Explorer. Under the VS Code Terminal Menu, select New Terminal. From the terminal prompt type in
dotnet new console -o CreateThreeAccounts

This will create an empty console project. To setup debugging for a C# project, open the Command Palette (View > Command Palette) by typing ‘.NET’, and running .NET: Generate Assets for Build and Debug. This command will generate the necessary launch.json and tasks.json configuration files (under the .vscode folder). You may see an error on OmniSharp, followed by a prompt to install additional assets, click yes, then try again.

.NET: Generate Assets for Build and Debug

Figure 4-1 .NET: Generate Assets for Build and Debug

5. Add the Algorand NuGet package

Open the CreateThreeAccounts folder and open Program.cs. In this step we will setup the NuGet Package Manager which is needed to integrate shared components and SDKs. Open the Command Palette (View > Command Palette) and type ‘NuGet’ and run Add NuGet Package. Enter Algorand when prompted. Select latest.

Add NuGet Package

Figure 5-1 Add NuGet Package for Algorand

6. Debugging C# with VS Code sample

In this step, we will show how to create a debug session for C# using VS Code. In order to run the code below, start up the sandbox or change the args below to your node URL and token. Infomation on setting up sandbox/nodes is in the Requirements section above. The steps are similar for all languages. Here is a sample C# file to test with. This code generates Accounts and retrieves the mnemonic.

Replace the code in Program.cs with the following code. This code generates three accounts along with the mnemonics.

using System;
using Account = Algorand.Account;

namespace Tutorials
{
    public class CreateThreeAccounts
    {
        public static void Main(params string[] args)
        {
            if (args == null)
            {
                Console.WriteLine("args is null"); // Check for null array
            }
            else
            {
                args = new string[2];
                args[0] = "http://localhost:4001";
                args[1] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            }

            Account myAccount = new Account();
            var myMnemonic = myAccount.ToMnemonic();
            Console.WriteLine("Account 1 Address = " + myAccount.Address.ToString());
            Console.WriteLine("Account 1 Mnemonic = " + myMnemonic.ToString());

            Account myAccount2 = new Account();
            var myMnemonic2 = myAccount2.ToMnemonic();
            Console.WriteLine("Account 2 Address = " + myAccount2.Address.ToString());
            Console.WriteLine("Account 2 Mnemonic = " + myMnemonic2.ToString());

            Account myAccount3 = new Account();
            var myMnemonic3 = myAccount3.ToMnemonic();
            Console.WriteLine("Account 3 Address = " + myAccount3.Address.ToString());
            Console.WriteLine("Account 3 Mnemonic = " + myMnemonic3.ToString());

            Console.WriteLine("You have successefully created 3 accounts.");
            Console.WriteLine("Dispense funds to these 3 accounts ");
            Console.WriteLine("see TestNet dispenser https://bank.testnet.algorand.network/");
        }
    }
}

Press Run Debug C# (F5)

Figure 6-1 Set breakpoint and Press Run (F5)

A) Select the Debug icon on the left

B) Set a breakpoint in the code. To set a breakpoint, simply click on the left margin, sometimes referred to as the gutter, on the line to break.

C) See the breakpoints set in the debug pane.

D) Select .NET Core Launch (console) from the dropdown, and press Run and Debug. You should hit your breakpoint. Open it to see the settings, it is located in the .vscode folder in your code folder. Each configuration should appear in the Debug and Run drop-down menu.

E) To view launch.json for the debug configuration press the settings gear. Optionally, to customize Run and Debug, click on “Add Configuration” on the launch.json page.

Your breakpoint should be hit, and the program execution paused on the line of your breakpoint. It is highlighted.

Debugging

Figure 6-2 Debugging.

We are now debugging! See Figure 6-2 above.

A) Local Variables

B) Hover over a variable with the cursor. See the Data Tip with the contents

C) Navigate by selecting run (to next breakpoint), step over, step into, step out of or stop


Learn More

Introductory VS Code Videos

Keyboard Shortcuts charts for:
Windows,
MacOS, and
Linux