Function to split either a string array or string into a string array I am programming a language interpreter in C# and have recently created a set of functions that receive either a string or a string[] and split it by a received string.

For example, with a string:

"Hey:123:hello:456"
":"


It will return this array:

{"hey","123","hello","456"}


And with a string[]:

{"a:b","c","d:e:f"}
":"


It will return this array:

{"a","b","c","d","e","f"}


How can I improve efficiency and maybe make it tidier?

public string[] SplitRows(object thevar, string delimiter) {

            if (delimiter == "&lt;newline&gt;") delimiter = "\n";

            if (thevar.GetType() == typeof(string)) {
                string temp = (string) thevar;
                return temp.Split(new string[] {
                    delimiter
                }, System.StringSplitOptions.None);
            }
            if (thevar.GetType() == typeof(string[])) {
                return stringarraysplitter((string[]) thevar, delimiter);
            }
            return null;
        }


        public string[] stringarraysplitter(string[] arr, string delimiter) {
            string[][] tempr = new string[arr.Length][];
            for (int i = 0; i &lt; arr.Length; i++) {
                tempr[i] = arr[i].Split(new string[] {
                    delimiter
                }, System.StringSplitOptions.None);
            }
            System.Collections.Generic.List &lt; string &gt; templist = new System.Collections.Generic.List &lt; string &gt; ();
            for (int i = 0; i &lt; tempr.Length; i++) {
                for (int j = 0; j &lt; tempr[i].Length; j++) {
                    templist.Add(tempr[i][j]);
                }
            }

            return templist.ToArray();
        }


Side notes:


SplitRows() is the only main function allowed to receive either a string array or string. Even though I use 2 functions, I cannot create 2 for the 2 variable types.
&lt;newline&gt; is a replacement string for \n in my language and can be ignored.
if (delimiter == "&lt;newline&gt;") delimiter = "\n";


Why do you need this? Why not use normal strings?

if (thevar.GetType() == typeof(string)) {


What's wrong with normal method overloading?

if (thevar.GetType() == typeof(string)) {...}
if (thevar.GetType() == typeof(string[])) {...}
return null;


If arguments is not valid throw an ArgumentException. Don't say "I'll just return null, and see where it will go wrong."

temp.Split(new string[] {delimiter}, System.StringSplitOptions.None);


This bit repeats, and should be extracted into a method.

for (int i = 0; i &lt; tempr.Length; i++) {
    for (int j = 0; j &lt; tempr[i].Length; j++) {


Use Linq to simplify loop operations.

Here is the cleaned up version:

    public IEnumerable&lt;string&gt; Split(string str, string delimiter)
    {
        return str.Split(new[]{delimiter}, StringSplitOptions.None);
    }

    public IEnumerable&lt;string&gt; Split(IEnumerable&lt;string&gt; arr, string delimiter) {
        return arr.SelectMany(s =&gt; Split(s, delimiter));
    }