Cosmin Ioniță
Cosmin Ioniță

Reputation: 4055

Convert string to an array of strings on the fly

I have a method that returns an array of strings. In that method I am processing only one string, and I want to return it as an array of strings (an array of strings with only one element, which is my string → array[0] == myString).

I want that because I want to avoid some ugly code for creating an array of strings with one element, like that:

private static string[] FooterContent()
{
      string[] array = new string[1];
      array[0] = GetMyData();
      return array;        
}

I want something like that:

private static string[] FooterContent()
{
      string myData = GetMyData();
      return myData.ToArray();        
}

And that myData.ToArray() should create an array with only one element (array[0] = myString) and then to return it.

Or, something like that (if possible):

private static string[] FooterContent()
{
     return GetMyData().ToArray();        
}

The point is that I can't simply modify the return type from string[] to string because I am using the value returned from FooterContent (array of strings) and from other similar methods, like BodyContent or HeadContent, into another general method, WriteContent() that accepts an array of strings.

Is there an elegant way of doing this?

Thank you respectfully.

Upvotes: 1

Views: 1035

Answers (4)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

Alternatively, you can wrap your GetData return value in a struct and use implicit operators to perform conversions:

public struct DataResult
{
    private string _raw;
    private string[] _rawArray;

    public DataResult(string raw)
    {
        _raw = raw;
        _rawArray = new [] { raw };
    }

    private string Raw { get { return _raw; } }
    private string[] RawArray { get { return _rawArray; } }

    public static implicit operator string(DataResult result)
    {
        return result.Raw;  
    }

    public static implicit operator DataResult(string rawResult)
    {
        return new DataResult(rawResult);
    }

     public static implicit operator string[](DataResult result)
     {
         return result.RawArray;
     }

    public static implicit operator DataResult(string[] rawResultArray)
    {
        if(rawResultArray == null || rawResultArray.Length != 1)
            throw new ArgumentException("Raw result must be a single item array", "rawResultArray");

        return new DataResult(rawResultArray[0]);
    }
}

Implicit operators let you do implicit conversions from apples to pears (and viceversa):

DataResult result = new DataResult("hello world");
string[] resultArray = result;
string rawResult = result;

DataResult result2 = new string[] { "hello world" };
DataResult result3 = "hello world";

What does it mean? If your GetData method returns DataResult any other code can set its value to a string, string[] or DataResult, and perform the opposite conversions implicitly.

I believe that in your case, this should be one of best approaches.

Upvotes: 1

Fka
Fka

Reputation: 6234

You can:

private static string[] FooterContent()
{
    return new[] { GetMyData() };        
}

or write extension method which isn't the best approach here (but possible):

public static class StringExtensions
{
    public static string[] ToSingleElementArray(this string inputString)
    {
        return new[] { inputString };
    }
}

private static string[] FooterContent()
{
    return GetMyData().ToSingleElementArray();        
}

Upvotes: 4

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

Try this,

private static string[] FooterContent()
{
     return new[] { GetMyData() };
}

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1503290

As w0lf suggested in comments, the simplest way of creating an array would just be:

return new[] { GetMyData() };

While you could create an extension method, I personally wouldn't - and if you do, you absolutely should not call it ToArray, as that already has a meaning for string due to Enumerable.ToArray and the fact that string implements IEnumerable<char>.

If you really, really want to create this extension method, I'd do it as:

public static T[] ToSingleElementArray(this T value)
{
    return new[] { value };
}

Then you can use:

return GetMyData().ToSingleElementArray();

But as I said, I wouldn't even create the extension method...

Upvotes: 8

Related Questions