In Focus

Getting Started With Smart Contracts In C#

In this article, I will explain the development tools and steps required to write your first smart contract in C# and Visual Studio 2017.

Smart Contracts In C#
 
In my previous article, Introduction to NEO Blockchain, we talked about what NEO blockchain is and its purpose. This article focuses on setting up a development environment with Visual Studio for the NEO blockchain smart contracts development using C#. 
 
To write a smart contract using C#, we need the following development tools.
  1. Visual Studio 2017 or later
  2. Install NEOContractPlugin
  3. Install neo-compiler
  4. Setup NEO execution path
  5. Create your first smart contract

Visual Studio 2017


You can use any edition - Enterprise, Professional, or Community version of Visual Studio 2017 for NEO smart contracts development. If you do not have Visual Studio 2017 or a later version installed, then download Visual Studio 2017 from here.
 
If you already have installed Visual Studio 2017 or later, make sure you’ve enabled the support for cross-platform development. If you’re not sure, go to your Visual Studio Update options and check the .NET Core cross-platform development checkbox and update your Visual Studio. 
 
 
 
NEO blockchain uses .NET Core, the new and open source version of .NET. If you do not have .NET Core installed, you will not be able to open neo-vm project, that is, the NEO virtual machine which you must have in order to compile and execute the blockchain smart contracts on NEO blockchain.
 
The installation process is simple. Just follow step by step instructions.
 

Install NEOContractPlugin

 
Once you’ve VS 2017 all set, it's time for NEOContractPlugin.
 
Open Visual Studio 2017 > Open Tools > Select Extensions and Updates > Select Online on the left tab > Now, search for NEO. You will get the NEOContractPlugin in search results. Select NEOContractPlugin and click "Download".
 
 
 
This will download and install NeoContractPlugin.
 

Install neo-compiler

 
Neo-compiler installation is also quite straight-forward. Download neo-compiler project from Github - https://github.com/neo-project/neo-compiler.
 
Once downloaded, go to your downloaded folder and open the solution in Visual Studio. Open Solution Explorer and publish the neon project.
 
 
  
After successful publishing, the neon.exe file will be generated in the provided directory. In our case, it’s in bin\Release\PublishOutput.
 
 

Setup up NEO execution path

 
Now, we’ll add the bin\Release\PublishOutput directory to our execution path. Open the Control Panel, click on System >> Advanced System Settings.
 
  
Click on the "Environment Variables" button and select the path from system variables and click on "Edit". Add the path and click OK.
 
Test it
 
To make sure the environment variable is set correctly, open the command prompt and enter neon. If it’s set correctly, you will get the compiler version.
 
 

Create and compile a NeoContract

 
NeoContract is the smart contract you build for NEO blockchain. Now, let’s create our first smart contract that can be deployed and executed on NEO blockchain.
 
Open Visual Studio and create a "New Project". Select Visual C# > NeoContract from the available project templates.
 
 
Enter your project name and click OK. This will create a project. I named my project "FirstNeoContract".
 
Open the Contract1.cs file and put following code in the main method.
 
 
Build and compile.
 
Now, if you go to your bin/debug folder, you will find a file named FirstNeoContract.avm. This is your smart contract written in C# that can be deployed on NEO Blockchain.
 
 
Congratulations!
 
You’ve successfully created a blockchain smart contract.
 

Summary

 
In this article, we learned how to set up our development machine for NEO blockchain smart contracts using Visual Studio and C#. In my next article, I will discuss smart contracts in more details and how we can deploy our smart contract on a NEO blockchain.