# Subset of C# programming language supported by gsharpc

gsharpc only supports the use of a subset of the C# programming language to write smart contracts, including most C# syntax and control structures, and also provides built-in functions and types similar to the uvm language class library. This article describes the syntax and library of supported C# subsets

##Basic syntax of C#

Refer to the official Microsoft documentation https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/programming-guide/index This article will describe which features are supported after this article

##C# version smart contract format

Format example:

using static UVMCoreLib.UVMCoreFuncs;  // Reference built-in global function library
using UVMCoreLib; //  Reference other built-in Libraries

public class Storage  // The storage type of the contract is defined here
{
    public string Name { get; set; } // Define properties of storage
    public int Age { get; set; }
    public string Country; // The storage attribute can also be defined with the field syntax of C#
    public bool IsMale { get; set; }
    // 这里定义string数组类型的storage属性,因为uvm的数组和C#数组用法不兼容,所以用UVMArray来代币uvm中的数组类型
    public UVMArray<string> ArrayDemo { get; set; } 
}

// 这是可选的,实现IUVMEventEmitter接口的类型定义了本合约中有哪些event事件需要抛出
public class MyEventEmitteer : IUVMEventEmitter
{
    // The static method at the beginning of "emit" has only one string parameter. The return //value is void, which represents an event. The event name is the method name without the //prefix "emit"
    public static void EmitHello(string eventArg)
    {
         // The method body of event event is thrown here, which can be debugged directly in C# project. When compile to UVM bytecode, it does not include the method body here.
        Console.WriteLine("event Hello emited, arg is " + eventArg);
    }
    public static void EmitHello2(string eventArg)
    {
        Console.WriteLine("event Hello2 emited, arg is " + eventArg);
    }
}

// 这里是具体的合约类型,类型名自定义,需要符合C#的类型名规范,需要继承UVMContract<T>类型,其中T类型表示合约的storage类型
public class MyContract : UVMContract<Storage>
{
    // 合约的构造函数,这个函数内不要添加方法体代码,增加了在编译到uvm字节码后也不会包含,只有C#项目直接运行时起效
    public MyContract() : base(new Storage())
    {
    }

    // 必须实现的init方法,合约注册的时候初始化合约的函数
    public override void init()
    {
        print("contract initing"); // 这里调用的print函数就是文件头using static引用的UVMCoreLib.UVMCoreFuncs中的静态方法
        this.storage.Age = 100; // 合约的storage的属性都需要在init方法中初始化,否则无法成功注册合约到链上
        this.storage.Country = "China";
        this.storage.Name = "C#";
        this.storage.IsMale = true;
        this.storage.ArrayDemo = UVMArray<string>.Create(); // 这里是创建一个空的元素类型是string类型的uvm数组
        this.storage.ArrayDemo.Add("hello"); // 给uvm数组中添加一个元素"hello"
        pprint(this);
        print("this is contract init api");
    }

    // 这里是定义了一个合约中的API,接受一个string类型参数,返回一个string类型对象
    public string GetAge(string arg)
    {
        print("this is contract getAge api");
        return "" + this.storage.Age; // 返回两个对象连接后的字符串,字符串和其他类型用+连接的时候,会自动调用tostring转换成字符串连接
    }

    // 定义一个返回string类型对象的offline接口,offline接口在链上通过call_contract_offline命令调用,调用操作不上链,主要用来定义查询类函数
    public string OfflineGetAge(string arg)
    {
        print("this is contract OfflineGetAge api");
        print("age is " + this.storage.Age); // 这里this.storage读取了本合约的storage对象
        return "" + this.storage.Age;
    }

    public void TestHello(string arg)
    {
        print("this is contract hello api with argument " + arg); // 这里访问了合约API的参数arg
    }
}

// 除了合约类型,storage类型外,还需要额外定义一个包含Main方法的类型,作为合约的加载入口
public class ExampleLibClass
{
    // 非静态的Main方法作为合约的加载入口,返回类型是定义的合约类型,这个方法会被调用用来加载合约,返回的合约对象就是本文件定义的合约
    public MyContract Main()
    {
        print("start of demo C# contract");
        var contract = new MyContract();
        print("end main");
        return contract; // 必须返回一个合约对象
    }
}

# Built-in library

The importModule(string moduleName) function can refer to the built-in module library, such as string, table, json modules, etc.

Some global functions are built-in, the list is as follows:

bool and(bool a, bool b) 
bool or(bool a, bool b) 
number div(number a, number b) 
int idiv(number a, number b) 
number neg(number a) 
bool not(bool a) 
void print(object obj) 
string tostring(object obj) 
string tojsonstring(object obj) 
void pprint(object obj) 
int? tointeger(object obj) 
float? tonumber(object obj) 
T importContract<T>(string contractName) 
T importModule<T>(string moduleName) 
void Debug() 
string Type(object value) 
void Exit(int exitCode) 
void Error(string errorMsg) 
UVMMap<object> getmetatable(UVMTable table) 
void setmetatable(UVMTable table, UVMTable metatable) 
bool toboolean(object value) 
UVMTable totable(object value) 
bool rawequal(object a, object b) 
long rawlen(object value) 
object rawget(object table, object key) 
void rawset(object table, object key, object value) 
int transfer_from_contract_to_address(string address,
  string assetName, long amount) 
void set_mock_contract_balance_amount(string contractAddress, 
  string assetName, long amount) 
long get_contract_balance_amount(string contractAddress, string assetName) 
long get_chain_now() 
long get_chain_random() 
long get_header_block_num() 
long get_waited(long num) 
string get_current_contract_address() 
string caller() 
string caller_address() 
long get_transaction_fee() 
long transfer_from_contract_to_public_account(string to_account_name, string assertName,
  long amount) 

# Examples of C# syntax supported by gsharpc

类型定义和使用

类型的属性和字段定义和使用

类型的成员方法,静态方法定义和使用

函数调用,函数参数,函数返回值的支持

类型继承

数值操作

字符串连接(通过+连接的方式只支持2个对象进行字符串连接)

布尔操作

变量声明和赋值

this对象的使用

if/else if/else控制流结构

for控制流结构

uvm的Map和Array类型的创建和修改

uvm的Map和Array类型的遍历(迭代器遍历)

while控制流结构

continue和break语句的支持

比较操作符

内置函数和库的调用

new 新类型

emit event

引用内置模块

引用合约