Ali3nat0r
Ali3nat0r

Reputation: 21

Globally defining a function in C#

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();

Upvotes: 1

Views: 4883

Answers (8)

TomTom
TomTom

Reputation: 62101

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.

Upvotes: 0

David
David

Reputation: 73564

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

Upvotes: 7

Jay
Jay

Reputation: 3355

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.

Upvotes: 0

archil
archil

Reputation: 39501

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
        }
    }
}

Upvotes: 0

eyossi
eyossi

Reputation: 4340

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

Upvotes: 0

jason
jason

Reputation: 241651

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.

Upvotes: 4

Jakub Konecki
Jakub Konecki

Reputation: 46008

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

Upvotes: 2

SLaks
SLaks

Reputation: 887509

  1. No; C# does not support top-level functions

  2. You're looking for extension methods.

Upvotes: 0

Related Questions