If you understand basic C# and want to understand how block chain works. Here is atoy version of blockchain written in C#.

In here, a block stands for one transaction. You can see how a block is being constructed and how a block relates to its previous block.
The most interesting part is you can tell how a block can be mined. Check out the Mine() method.

It is really about just finding out the hash matching certain pattern. In here, it is about matching certain number of leading 0s in the final hash.

[code lang=”csharp”] class Block
{
public int Index { get; set; }
public string PreviousHash { get; set; }
public string Timestamp { get; set; }
public string Data { get; set; }
public string Hash { get; set; }

int _salt;

public Block(int index, string timestamp, string data, string previousHash = "")
{
this.Index = index;
this.PreviousHash = previousHash;
this.Timestamp = timestamp;
this.Data = data;
this.Hash = this.CalculateHash();
this._salt = 0;
}

// Create the hash of the current block.
public string CalculateHash()
{
var hash = SHA256_hash(this.Index + this.PreviousHash + this.Timestamp + this.Data + this._salt);

return hash;
}

// This is how the mining works!
public void Mine(int difficulty)
{
// You mined successfully if you found a hash with certain number of leading ‘0’s
// difficulty defines the number of ‘0’s required
// e.g. if difficulty is 2, if you found a hash like 00338500000x…, it means you mined it.
while (this.Hash.Substring(0, difficulty) != new String(‘0’, difficulty))
{
this._salt++;
this.Hash = this.CalculateHash();
Console.WriteLine("Mining:" + this.Hash);
}

Console.WriteLine("Block has been mined: " + this.Hash);
}

// Create a hash string from stirng
static string SHA256_hash(string value)
{
var sb = new StringBuilder();

using (SHA256 hash = SHA256Managed.Create())
{
var enc = Encoding.UTF8;
var result = hash.ComputeHash(enc.GetBytes(value));

foreach (var b in result)
sb.Append(b.ToString("x2"));
}

return sb.ToString();
}
}[/code]

A Blockchain contains numbers of blocks. Its main task is to build the chain and validate it.
Check out ValidateChain() method.

[code lang=”csharp”] class Blockchain
{
// a chain has many blocks
List<Block> _chain;
public int Difficulty { get; set; }

public Blockchain()
{
this._chain = new List<Block>();
this._chain.Add(CreateGenesisBlock());
this.Difficulty = 2;
}

Block CreateGenesisBlock()
{
return new Block(0, "01/12/2017", "Genesis block", "");
}

public Block GetLatestBlock()
{
return this._chain.Last();
}

// Once a block is mined, add it to the block
public void AddBlock(Block newBlock)
{
newBlock.PreviousHash = this.GetLatestBlock().Hash;
newBlock.Mine(this.Difficulty);

this._chain.Add(newBlock);
}

public void ValidateChain()
{

for (var i = 1; i < this._chain.Count; i++)
{
var currentBlock = this._chain[i];
var previousBlock = this._chain[i – 1];

// Check if the current block hash is consistent with the hash calculated
if (currentBlock.Hash != currentBlock.CalculateHash())
{
throw new Exception("Chain is not valid! Current hash is incorrect!");
}

// Check if the Previous hash match the hash of previous block
if (currentBlock.PreviousHash != previousBlock.Hash)
{
throw new Exception("Chain is not valid! PreviousHash isn’t pointing to the previous block’s hash!");
}
}
}
}[/code]

Download the source code on GitHub