Introduction
Blockchain technology is the foundation of Bitcoin, the most popular cryptocurrency in the world. With the popularity of Bitcoin, Blockchain has also got great exposure. People have now started to use Blockchain in other-than-cryptocurrency kinds of applications too. Looking at Satoshi Nakamoto's (the founder of blockchain) Bitcoin whitepaper, you could get confused about how bitcoin works. Today, I am going to build a blockchain from scratch to help everybody understand the mechanism of the blockchain.
What is Blockchain?
Blockchain is a database. What is a database? A database is an organized collection of data. Or, you can say, a data structure that stores the data. Therefore, Blockchain is just a data structure that stores the data. As the name hinted, there will be a chain of blocks.
Chain of Blocks
Below is the architecture diagram of a basic blockchain.

This basic blockchain has a linked list that is composed of blocks. Each block has the following properties.
- Index
- Timestamp
- Previous Hash
- Hash
- Data
The first block is a special block: genesis block. The genesis block is the only block that has no previous blocks and does not contain data.
Implementation
We will add two classes for this data structure: Block and Blockchain.
Block
public class Block
{
public int Index { get; set; }
public DateTime TimeStamp { get; set; }
public string PreviousHash { get; set; }
public string Hash { get; set; }
public string Data { get; set; }
public Block(DateTime timeStamp, string previousHash, string data)
{
Index = 0;
TimeStamp = timeStamp;
PreviousHash = previousHash;
Data = data;
Hash = CalculateHash();
}
public string CalculateHash()
{
SHA256 sha256 = SHA256.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes($"{TimeStamp}-{PreviousHash ?? ""}-{Data}");
byte[] outputBytes = sha256.ComputeHash(inputBytes);
return Convert.ToBase64String(outputBytes);
}
}
Blockchain
public class Blockchain
{
public IList<Block> Chain { set; get; }
public Blockchain()
{
InitializeChain();
AddGenesisBlock();
}
public void InitializeChain()
{
Chain = new List<Block>();
}
public Block CreateGenesisBlock()
{
return new Block(DateTime.Now, null, "{}");
}
public void AddGenesisBlock()
{
Chain.Add(CreateGenesisBlock());
}
public Block GetLatestBlock()
{
return Chain[Chain.Count - 1];
}
public void AddBlock(Block block)
{
Block latestBlock = GetLatestBlock();
block.Index = latestBlock.Index + 1;
block.PreviousHash = latestBlock.Hash;
block.Hash = block.CalculateHash();
Chain.Add(block);
}
}
After we have those two classes, we can create an instance of the new blockchain.



Carnation FlowersPosted Jul 5, 2022, 9:47 AM
Thanks, can you also give me example how can I use it in real life like if I want to add it my website ...etc
Tay NguyenPosted Aug 13, 2021, 9:14 AM
It is very helpful!
Ivandro JaoPosted Dec 20, 2019, 7:58 AM
Thank you so much, I find your example very concise :)
Muhammad SaleemPosted Jul 16, 2019, 6:37 PM
plz help me how can i fix this Error : The default XML namespace of the project must be the MSBuild XML namespace. If the project is authored in the MSBuild 2003 format, please add xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to the <Project> element. If the project has been authored in the old 1.0 or 1.2 format, please convert it to MSBuild 2003 format.
Hina SiddiquiPosted May 12, 2019, 1:59 PM
Hi, i create a fund chariy system based on blockchain.i'm new in blockchain please provide me guideness how to implement this thing in MVC.
Sigmond GattPosted Apr 22, 2019, 12:04 PM
HI , i followed this tutorial and everything worked perfectly , the only problem is that i need somewhere to store my blockchain , could you help please , I was told as well that to store the blockchain you need to create your own serializer etc... I don't know if that is correct, can you help me please ?
Dev JagPosted Feb 27, 2019, 5:36 AM
Hello, I am not a dev, but want to build a software where I could use BC (partly) to secure documents get shared. I am trying to learn C# but its hard. I am looking for help to create the app, which I have already done the Form creation in C# VS2017. Please let me know if any dev here has the time to do the form coding. Thanks, Dew
Asif AkbarPosted Feb 6, 2019, 1:36 AM
Shouldnt genesis block be checked for if already existed?
Jin ShiPosted Feb 5, 2019, 4:30 PM
Hi Henry, great tutorial. Not sure if you agree but i think in the IsValid() method, the second if statement should be "if (currentBlock.PreviousHash != previousBlock.CalculateHash())". The way you have it, that if statement will never return false.
Shawn MacPosted Dec 20, 2018, 2:57 PM
Curious why don't you store the blocks in a lightweight database?
Gaurav PrakashPosted Jul 3, 2018, 3:20 AM
The IsValid() method loops through all the blocks in the chain, what happens to performance when the chain becomes too large?
Bhavesh JadavPosted Jun 10, 2018, 11:39 PM
Excellent Sir, i like it.
Saad ShaikhPosted Jun 3, 2018, 12:38 AM
Excellently written article. One question, the validation you performed here is on one computer(node), how would we validate blocks from/with other computers(nodes) on network?
Tushar DikshitPosted May 30, 2018, 4:21 PM
Nice post, informative.
Naresh SinghalPosted May 26, 2018, 1:13 AM
Fantastic Sir!! Excellent....
Giulio EffePosted May 25, 2018, 7:07 PM
Hi Henry, I made a similar project and open sourced it ! Take a look if you want : https://github.com/frontegi/DocsChain
Dennis LandiPosted May 25, 2018, 5:40 PM
One Note: Where you use "DateTime.Now", I recommend using "DateTime.UtcNow"
Akshay DeshmukhPosted May 25, 2018, 5:08 AM
Explained very well; clean and clear!
Mahesh ChandPosted May 24, 2018, 5:49 AM
Welcome to C# Corner, Henry. Good start and looking forward to your next articles. Cheers!
Ch.Smrutiranjan ParidaPosted May 24, 2018, 4:40 AM
Nice Article.Good to start with for a Blockchain.
Satyaprakash SamantarayPosted May 24, 2018, 3:11 AM
It is very helpful article . Thanks for sharing....
Kasam ShaikhPosted May 23, 2018, 7:01 PM
Interesting, would be helpful!