Title: Can I create int.FromBytes(byte[] bytes) extension method in C#?
Tags: c#;extension-methods;c#-7.3
Question: Can I create int.FromBytes(byte[] bytes) extension method in C#?

I need something with usage like this:

```int a = int.FromBytes(new byte[]{1,2,3,4});
```

I'm using C# 7.3.



Actually I need this for ushort data type. For now I'm using extension method like this:

```public static ushort FromBytes(sbyte msb, byte lsb)
{
    ushort usmsb = (byte)msb;
    ushort uslsb = lsb;

    return (ushort)((usmsb << 8) + uslsb);
}
```

I'm using it like this:

```ushort x = Helpers.FromBytes(1, 2);
```



I can't answer my closed question, so I post it here. This is how I did it and what I needed:

```// two byte tuple extension
public static ushort ToUShort(this (byte msb, byte lsb) bytes)
{
    ushort usmsb = bytes.msb;
    ushort uslsb = bytes.lsb;
    return (ushort)((usmsb << 8) + uslsb);
}
```

Usage:

```byte byte1 = 32;
byte byte2 = 42;
ushort result = (byte1, byte2).ToUShort();
```

This is much better than extension for byte[] because you can't pass wrong number of bytes.

Comment: @ScottHunter Nothing happened. I don't know the syntax to create extension like this.

Comment: @Han RIght. I have extension method to convert it back. I messed up my question.

Comment: @RenéVogt I know about BitConverter, byt my question is about language.

Comment: @Flydog57 This is exactly what I want, I have this for conversion back, but I didn't thought about this this obvious way :) Can you post this as an answer?

Comment: Your FromBytes() is not an extension method. It's just a static method.

Comment: You should really read the duplicate question before stating that your question isn't a duplicate, given that it's asking for exactly what you claim to be asking, and gives you exactly the answer you claim you want.

Comment: That FromBytes method is (as shown) a static method on int. You can't currently write static extension methods. You could write an extension method on byte arrays (or even `IEnumerable`. Something like `new byte[] {1, 2, 3, 4}.ToInt()`)

Comment: You don't need to, it's already [there](https://docs.microsoft.com/de-de/dotnet/api/system.bitconverter.toint32?view=netframework-4.8)

Comment: What happened when you tried?


Here is another answer: Yes, you can map to the existing static method with your own static extension method, if you think it is worth the trouble.

```static public class ExtensionMethods
{
    static public int ToInt32(this byte[] source)
    {
        return BitConverter.ToInt32(source, 0);
    }
}
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 9, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:07/05/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><extension-methods><c#-7.3>', 'Title': 'Can I create int.FromBytes(byte[] bytes) extension method in C#?', 'ViewCount': 185, 'id': 56906986, 'user_AboutMe': '<p>Electrican by education\nElectronics engineer by hobby\nProgrammer (some languages professionally, some by hobby)\nWindows Server admin - few years of experience</p>\n\n<p>Sorry for my grammar and spelling errors, feel free to correct them by editing my posts.</p>\n', 'user_AccountId': 1255814.0, 'user_DisplayName': 'Kamil', 'user_Reputation': 12955.0, 'user_Views': 2225.0, 'user_id': 1215291.0}}