Now go to your .NET app folder and open the Program.cs C# file in a text editor and edit the code according to the following code.
using System;
using System.Threading.Tasks;
using Nethereum.Web3;
namespace nethereumapp
{
class Program
{
static void Main(string[] args)
{
GetBlockNumber().Wait();
}
static async Task GetBlockNumber()
{
var web3 = new Web3("ADD_YOUR_ETHEREUM_NODE_URL");
var latestBlockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync();
Console.WriteLine($"Latest Block Number is: {latestBlockNumber}");
}
}
}
using System;
using System.Threading.Tasks;
using Nethereum.Web3;
namespace nethereumapp
{
class Program
{
static void Main(string[] args)
{
GetBlockNumber().Wait();
}
static async Task GetBlockNumber()
{
var web3 = new Web3("ADD_YOUR_ETHEREUM_NODE_URL");
var latestBlockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync();
Console.WriteLine($"Latest Block Number is: {latestBlockNumber}");
}
}
}
using System;
using System.Threading.Tasks;
using Nethereum.Web3;
namespace nethereumapp
{
class Program
{
static void Main(string[] args)
{
GetBlockNumber().Wait();
}
static async Task GetBlockNumber()
{
var web3 = new Web3("ADD_YOUR_ETHEREUM_NODE_URL");
var latestBlockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync();
Console.WriteLine($"Latest Block Number is: {latestBlockNumber}");
}
}
}
Make sure to replace `ADD_YOUR_ETHEREUM_NODE_URL` with the http provider from the section above.
Explanation of the code above
Line 1-3: Adding the required namespaces.
Line 5: Declaring our app’s namespace (Which will have a class collection).
Line 7: Declaring a class named Program.
Line 9-12: Declaring the Main class, which is the C# program’s entry point, and states what class does when executed. Declaring that the main class must wait for the execution of GetBlockNumber class.
Line 14: Declaring a Task class GetBlockNumber, task classes in C# are executed asynchronously.
Line 16: Creating an instance of Web3 and setting our node URL.
Line 17: Using the Eth API, we can execute the GetBlockNumber request asynchronously and storing the result in latestBlockNumber variable.
Line 18: Printing the latest block number stored in latestBlockNumber with a string “Latest Block Number is:”
Now to run your app, open your terminal/cmd, go to your .NET app’s folder and type the following.
$ dotnet run
$ dotnet run
If everything goes well and the code gets executed successfully, you must see an output like this: