1

I have two functions which I seem to be including in software quite a lot to convert strings and byte arrays between types. The usage is something like this:

string str = "Hello world";
byte[] b = strToByteArray(str);

Firstly, is there a way I can include this in the System namespace so I don't have to define it in code anymore?

Secondly, is there a way to define it as part of the string class, so the usage would be more like:

string str = "Hello world";
byte[] b = str.ToByteArray();
Ali3nat0r
  • 21
  • 1
  • 2

8 Answers8

7

What you're asking for is provided in C# by Extension Methods.

David
  • 72,686
  • 18
  • 132
  • 173
  • Extension methods can only add instance methods to existing class. If you want a "near" global method (no instance), you have to create a class with static method – Steve B May 30 '12 at 15:41
  • 1
    YOu could extend string and byte[] ;) – TomTom May 30 '12 at 15:47
  • 1
    @SteveB Actually, you can call an extension method statically as well: `Extensions.MyExtension("some string")` instead of `"some string".MyExtension()`. @TomTom `string` is `sealed`, and `byte[]` isn't valid as a base type, so no you can't do that. – Tim S. May 30 '12 at 15:57
4

Why aren't you using System.Text.Encoding.Unicode.GetBytes?

Firstly, is there a way I can include this in the System namespace so I don't have to define it in code anymore?

I don't know what you mean by "define it in code anymore". You can add anything you want to System, but that's generally frowned upon. Just say

namespace System {
    // add classes here
}

C# doesn't have top-level methods like you seem to desire. Eric Lippert has discussed this before.

Secondly, is there a way to define it as part of the string class, so the usage would be more like:

I suppose that you could define an extension method on string:

public static class StringExtensions {
    public static byte[] GetBytes(this string s) {
        // do something and return byte[]
    }
}

If you want, put it in the namespace System. Again, this is generally frowned upon, and you still have to reference the assembly that contains the definition.

jason
  • 236,483
  • 35
  • 423
  • 525
  • Thanks, extension methods are pretty much what I was after. By "define it in code anymore" I meant add it to the standard includes so I don't have to add it to every new project. – Ali3nat0r May 30 '12 at 19:26
2

Don't reinvent the wheel, Use Encoding class instead.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
0
  1. No; C# does not support top-level functions

  2. You're looking for extension methods.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

You can write an extension method for string that does what you need: http://csharp.net-tutorials.com/csharp-3.0/extension-methods/

and then you can just call it with every string:

byte[] b = "Hello world".ToByteArray();

for the implementation you can use Encoding.GetBytes by the way

eyossi
  • 4,230
  • 22
  • 20
0

All you need is custom Extension Method within namespace System.

namespace System
{
    public static class StringExtensions
    {
        public static byte[] ToByteArray(this string source)
        {
            // return result
        }
    }
}
archil
  • 39,013
  • 7
  • 65
  • 82
0

See Why use the global keyword in C#? for additional information... You can add a using statement to achieve the semantic you are looking for or you can use the global keyword.

Community
  • 1
  • 1
Jay
  • 3,276
  • 1
  • 28
  • 38
0

Firstly, is there a way I can include this in the System namespace so I don't have to define it in code anymore?

??? What do you want?

System namespace? Put them into a (static) calass in the system namespace. Namespaces have NOTHING to do with assemblies, dll's etc. - you can use whatever you like.

Oterhwise, do what most people do - start a project with tools classes. I have some of them around that I use for common code.

TomTom
  • 61,059
  • 10
  • 88
  • 148