QANplatform
qanplatform.comIntro to QANplatformTechnology FeaturesQANX TokenDev Docs
  • qanplatform.com
  • Welcome
  • Community
    • Social media
    • Newsletter
  • TECHNOLOGY
    • 🟢Intro to QANplatform
    • Technology Features
      • Hybrid blockchain
      • Multi-language Smart Contracts | Hyperpolyglot
      • Ethereum EVM Compatibility
      • Integrations
      • Proof-of-Randomness (PoR) consensus algorithm
      • Transaction per second (TPS)
      • Developer Royalty Fees
      • Mobile Phone Validation
      • Low-cost fixed transaction fees
      • Quantum-resistant Security
    • Use Cases
    • Blockchain 101
      • Blockchain Basics
      • Crypto Wallets
      • Coin and Token Types
      • Blockchain Transactions
  • QANX Token
    • What is QANX?
    • How to Buy QANX?
      • Buy QANX on PancakeSwap
      • Buy QANX on Uniswap
      • Buy QANX on Gate.io
      • Buy QANX on BitMart
      • Buy QANX on BingX
    • How to Store QANX
      • Store QANX in MetaMask
        • How to add QANX token to MetaMask
      • Store QANX in Trust Wallet
        • How to add QANX token to Trust Wallet
      • Store QANX in MEW
        • How to add QANX token to MEW
      • Other wallets
  • DEVELOPERS
    • QAN Private Blockchain
    • QAN TestNet
    • [QVM] Multi-language smart contracts
      • Generic workflow
        • Common API
        • Installing qvmctl
        • Setting up your workspace
        • Writing a smart contract
        • Compiling a smart contract
        • Deploying a smart contract
        • Calling a smart contract function
        • Reading smart contract storage
      • DOCs for supported languages
        • JavaScript (JS) smart contract
          • Writing a smart contract in JavaScript (JS)
          • Compiling a smart contract in JavaScript (JS)
        • Java smart contract
          • Writing a smart contract in Java
          • Compiling a smart contract in Java
        • Python smart contract
          • Writing a smart contract in Python
          • Compiling a smart contract in Python
        • TypeScript (TS) smart contract
          • Writing a smart contract in TypeScript (TS)
          • Compiling a smart contract in TypeScript (TS)
        • C# (C-Sharp) smart contract
          • Writing a smart contract in C# (C-Sharp)
          • Compiling a smart contract in C# (C-Sharp)
        • C++ smart contract
          • Writing a smart contract in C++
          • Compiling a smart contract in C++
        • C smart contract
          • Writing a smart contract in C
          • Compiling a smart contract in C
        • Golang (Go) smart contract
          • Writing a smart contract in Golang (Go)
          • Compiling a smart contract in Golang (Go)
        • Rust smart contract
          • Writing a smart contract in Rust
          • Compiling a smart contract in Rust
        • Kotlin smart contract
          • Writing a smart contract in Kotlin
          • Compiling a smart contract in Kotlin
      • [QVM] Versions & Changelog
        • V0.0.1
        • V0.0.2
    • Smart Contract Developers
    • Validators
    • Node Providers
  • ABOUT US
    • Company
    • Roadmap
    • Press kit & Media assets
    • Media mentions
    • Blog
  • Papers
    • White Paper
    • Pitch Deck
    • Onepager
    • Ebooks
      • Quantum-computing and Blockchain: The Definitive Guide
      • Blockchain's Energy Consumption: The Definitive Guide
      • Blockchain's Transaction Speed: The Definitive Guide
  • Audits
    • QANX Bridge Audit
    • QANX Token Audit
    • QAN TestNet Audit
  • Disclaimers
    • Disclaimer
      • Validation
      • Privacy Policy
      • Cookie Policy
Powered by GitBook
On this page
  • Sample contract functionality
  • Sample code
  • Save the contract
  1. DEVELOPERS
  2. [QVM] Multi-language smart contracts
  3. DOCs for supported languages
  4. C# (C-Sharp) smart contract

Writing a smart contract in C# (C-Sharp)

Sample contract functionality

The sample contract serves as a demonstration of general QVM logic.

It is capable of:

  • registering users into the database

  • storing the last registered user's name in the database

  • echoing the previous user's name to STDOUT

  • incrementing the total registered user count

  • storing above counter in the database

Sample code

using System;

public class Contract
{
    private static String getEnv(String key, String deft) {
        String? val = Environment.GetEnvironmentVariable(key);
        if (val == null || val.Length == 0) {
            return deft;
        }
        return val;
    }

    private static int atoi(String val) {
        try
        {
            return Int32.Parse(val);
        }
        catch (FormatException)
        {
            return 0;
        }
    }

    public static int Main(String[] args)
    {
        // THIS SAMPLE ONLY SUPPORTS THE "register" FUNCTION
        if (args.Length == 2 && args[0] == "register") {
           // GET THE CURRENT USER'S NAME OR DEFAULT TO "unknown" IF THIS IS THE FIRST CALL
           String? previousName = getEnv("DB_USER_CURRENT", "unknown");
           // GET THE TOTAL USER COUNT
           int totalUserCount = atoi(getEnv("DB_TOTALUSERS", "0"));
           // WRITE PREVIOUS USER NAME TO STDOUT
           Console.Out.WriteLine($"OUT=prevname: {previousName}");
           // UPDATE CURRENT USER NAME BY WRITING IT TO DB
           Console.Out.WriteLine($"DBW=USER_CURRENT={args[1]}");
           // STORE USER NAME UNDER A STORAGE SLOT FOR PERSISTENCE (CURRENT GETS OVERWRITTEN ON EACH CALL)
           Console.Out.WriteLine($"DBW=USER_{totalUserCount}={args[1]}");
           // INCREMENT THE TOTAL USER COUNT
           Console.Out.WriteLine($"DBW=TOTALUSERS={totalUserCount+1}");
           return 0;
        }
        if (args.Length >= 1) {
            Console.Error.WriteLine("Wrong CMD: " + args[0]);
            return 1;
        }
        Console.Error.WriteLine("Wrong args!");
        return 1;
    }
}

Save the contract

Open a text editor and save above sample contract as main.cs file.

PreviousC# (C-Sharp) smart contractNextCompiling a smart contract in C# (C-Sharp)

Last updated 1 year ago

Compiling a smart contract in C# (C-Sharp)