<p>You can find an example <a href="http://www.codeproject.com/Articles/835098/NBitcoin-Build-Them-All#simple" rel="nofollow">here</a>.</p>

<p>Specifically, see the first example where Alice sends bitcoins to Satoshi:</p>

<pre><code>Transaction aliceFunding = new Transaction()
{
   Outputs =
   {
       new TxOut("0.45", alice.GetAddress()),
       new TxOut("0.8", alice.Key.PubKey)
   }
};
Coin[] aliceCoins = aliceFunding
                    .Outputs
                    .Select((o, i) =&gt; new Coin(new OutPoint(aliceFunding.GetHash(), i), o))
                    .ToArray();
</code></pre>

<p>Notice the outputs (Alice's initial coins). The first uses GetAddress() to get the bitcoin address corresponding to Alice's private key (i.e. P2PKH) while the second uses Alice's public key (i.e. P2PK).</p>

<p>And the transaction is constructed here:</p>

<pre><code>var txBuilder = new TransactionBuilder();
var tx = txBuilder
    .AddCoins(aliceCoins)
    .AddKeys(alice.Key)
    .Send(satoshi.GetAddress(), "1.00")
    .SendFees("0.001")
    .SetChange(alice.GetAddress())
    .BuildTransaction(true);
Assert(txBuilder.Verify(tx)); //check fully signed
</code></pre>

CC BY-SA 3.0
2016-04-05T11:04:34.410Z
Thank you for your answer, In that example in codeproject they are sending colored coins,  I did try the example and when I send it to the blockchain I get an error message saying - malformed transaction.
gideonlouw
CC BY-SA 3.0
2016-04-05T11:30:42.657Z
The link takes you directly to a simple example (with a heading starting with "P2PK, P2PKH, Multi Sig payment"). The colored coins example is much further down. I edited my answer to include the example that I was referring to. If that is the one you tried and didn't work I might try later to run the example myself.
karask
CC BY-SA 3.0
2016-04-05T12:09:09.720Z
ok for the variable aliceFunding, should I load my txn of the latest transaction for "Alice" ?  

e.g. 

var blockr = new BlockrTransactionRepository();
Transaction aliceFunding =
blockr.Get("4ebf7f7ca0a5dafd10b9bd74d8cb93a6eb0831bcb637fec8e8aabf842f1c2688");
gideonlouw
CC BY-SA 3.0
2016-04-05T12:39:07.323Z
Yes. By the way, another resource that could be useful (in case you don't know it already) can be found in this [ebook](https://aois.blob.core.windows.net/public/Blockchain%20Programming%20in%20CSharp.pdf).
karask
CC BY-SA 3.0
2016-04-05T10:52:53.387Z
2016-04-05T11:28:41.157Z
2016-04-05T11:28:41.157Z
karask
<p>ok I updated the code:</p>

<pre><code>//Load latest transaction:
        var blockr = new BlockrTransactionRepository();

        NBitcoin.uint256 check = new NBitcoin.uint256("4ebf7f7ca0a5dafd10b9bd74d8cb93a6eb0831bcb637fec8e8aabf842f1c2688");

        Transaction aliceFunding = blockr.Get(check);


        Coin[] aliceCoins = aliceFunding
                                .Outputs
                                .Select((o, i) =&gt; new Coin(new OutPoint(aliceFunding.GetHash(), i), o))
                                .ToArray();

        txBuilder = new TransactionBuilder();
        tx = txBuilder
            .AddCoins(aliceCoins)
            .AddKeys(alice.Key)
            .Send(satoshi.GetAddress(), "0.05")
            .SendFees("0.001")
            .SetChange(alice.GetAddress())
            .BuildTransaction(true);


        Assert(txBuilder.Verify(tx)); 
</code></pre>

CC BY-SA 4.0
2019-10-16T13:49:54.990Z
How do you do that with a variable number of send addresses, ie, outside the fluent block?
ThirdPrize
CC BY-SA 3.0
2016-04-05T12:32:05.493Z
2016-04-05T12:32:05.493Z
gideonlouw
<p>I am using <code>NBitcoin</code> library to create my private keys and addresses, but I can't find an example of how to send Bitcoin.</p>

<p>Does anyone have a <code>c#</code> example code to send Bitcoin using the <code>NBitcoin</code> library ?</p>

<sending><c#><nbitcoin>
How can i send bitcoin using c# and the nbitcoin library?
gideonlouw
# How can i send bitcoin using c# and the nbitcoin library?

(Asked by: username_0 on 2016-04-05)

I am using `NBitcoin` library to create my private keys and addresses, but I can't find an example of how to send Bitcoin.

Does anyone have a `c#` example code to send Bitcoin using the `NBitcoin` library ?

**Tags:** sending, c#, nbitcoin


---

## 2 answers

### Answer by username_1 on 2016-04-05. Score: 1

You can find an example [here](http://www.codeproject.com/Articles/835098/NBitcoin-Build-Them-All#simple).

Specifically, see the first example where Alice sends bitcoins to Satoshi:

```
Transaction aliceFunding = new Transaction()
{
   Outputs =
   {
       new TxOut("0.45", alice.GetAddress()),
       new TxOut("0.8", alice.Key.PubKey)
   }
};
Coin[] aliceCoins = aliceFunding
                    .Outputs
                    .Select((o, i) => new Coin(new OutPoint(aliceFunding.GetHash(), i), o))
                    .ToArray();

```

Notice the outputs (Alice's initial coins). The first uses GetAddress() to get the bitcoin address corresponding to Alice's private key (i.e. P2PKH) while the second uses Alice's public key (i.e. P2PK).

And the transaction is constructed here:

```
var txBuilder = new TransactionBuilder();
var tx = txBuilder
    .AddCoins(aliceCoins)
    .AddKeys(alice.Key)
    .Send(satoshi.GetAddress(), "1.00")
    .SendFees("0.001")
    .SetChange(alice.GetAddress())
    .BuildTransaction(true);
Assert(txBuilder.Verify(tx)); //check fully signed

```

> **Comments:**
> **username_0**: Thank you for your answer, In that example in codeproject they are sending colored coins,  I did try the example and when I send it to the blockchain I get an error message saying - malformed transaction.  
> **username_1**: The link takes you directly to a simple example (with a heading starting with "P2PK, P2PKH, Multi Sig payment"). The colored coins example is much further down. I edited my answer to include the example that I was referring to. If that is the one you tried and didn't work I might try later to run the example myself.  
> **username_0**: ok for the variable aliceFunding, should I load my txn of the latest transaction for "Alice" ?    e.g.   var blockr = new BlockrTransactionRepository(); Transaction aliceFunding = blockr.Get("4ebf7f7ca0a5dafd10b9bd74d8cb93a6eb0831bcb637fec8e8aabf842f1c2688");  
> **username_1**: Yes. By the way, another resource that could be useful (in case you don't know it already) can be found in this [ebook](https://aois.blob.core.windows.net/public/Blockchain%20Programming%20in%20CSharp.pdf).  

---

### Answer by username_0 on 2016-04-05. Score: 0

ok I updated the code:

```
//Load latest transaction:
        var blockr = new BlockrTransactionRepository();

        NBitcoin.uint256 check = new NBitcoin.uint256("4ebf7f7ca0a5dafd10b9bd74d8cb93a6eb0831bcb637fec8e8aabf842f1c2688");

        Transaction aliceFunding = blockr.Get(check);

        Coin[] aliceCoins = aliceFunding
                                .Outputs
                                .Select((o, i) => new Coin(new OutPoint(aliceFunding.GetHash(), i), o))
                                .ToArray();

        txBuilder = new TransactionBuilder();
        tx = txBuilder
            .AddCoins(aliceCoins)
            .AddKeys(alice.Key)
            .Send(satoshi.GetAddress(), "0.05")
            .SendFees("0.001")
            .SetChange(alice.GetAddress())
            .BuildTransaction(true);

        Assert(txBuilder.Verify(tx)); 

```

> **Comments:**
> **username_2**: How do you do that with a variable number of send addresses, ie, outside the fluent block?  

---

