-5

This is a hypothetical method that allows arrays to use functions a single object can use. This would allow arrays to use methods that are only available to a single object. I just need a generic method that runs on all types of arrays. This method will run another method on the individual values of that array. Then those individual values will be returned as an array.This would work with all types. This method, if possible, should not have any constraints to which objects can use it. Repeat, there is no constraint any where in the code. Is this possible in another form? Is this legal in C#?

string[] s = new string[] { "hi", "hello", "what's up" }
static void Main(string[] args)
{
    string[] newS = s.ArrayTo(() => string.Remove(0,1));
    foreach(string str in newS)
        Console.WriteLine(str);
}
static class Ext
{
    static T[] ArrayTo<T>(this T[] t,Action a)
    {
        List<T> ret = new List<T>();
        foreach(T tOb in t)
        {
            ret.Add(
            //t.a());  This line doesn't work
        }
        return ret.ToArray();
    }
}

Output: i , ello , hat's up

Bilal Shafi
  • 115
  • 2
  • 13
  • Um... Please explain the down votes. – Bilal Shafi May 29 '17 at 00:47
  • your question it is not clear, please try to edit your answer, what did you mean with : allows arrays to function a single objects easily? We will glad to help you – Zinov May 29 '17 at 00:47
  • Sorry, I couldn't find another way to explain this without code. Please read my code to see what I mean. – Bilal Shafi May 29 '17 at 00:49
  • Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the "this" modifier. You mus specify a type – Zinov May 29 '17 at 00:52
  • Possible duplicate of [Why can't System.Array be a type constraint?](https://stackoverflow.com/questions/14795017/why-cant-system-array-be-a-type-constraint) – Zinov May 29 '17 at 00:57
  • I want this to also work with other types. So if I had a different object this method would allow that object array to use it's methods, not just strings, that is for the example. Also, I have a feeling this is not legal in C# so I need confirmation on that as well. It is not a duplicate because that was about constraints, this has no constraints, and if possible can be used on any objects. – Bilal Shafi May 29 '17 at 00:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145328/discussion-between-zinov-and-bilal-shafi). – Zinov May 29 '17 at 01:05

1 Answers1

0

Do not use this answer, just use Select() method much simpler.

class Program
{
    static void Main(string[] args)
    {
        string[] s = new string[] { "hi", "hello", "what's up" };
        string[] newS = s.ArrayTo<string>(x => x.Remove(0, 1) );
        foreach (string str in newS)
            Console.WriteLine(str);
    }
}
static class Ext
{
    public static T[] ArrayTo<T>(this T[] t, Func<T,T> a)
    {
        List<T> ret = new List<T>();
        foreach (T tOb in t)
        {
            ret.Add(
                a(tOb));
        }
        return ret.ToArray();
    }
}
Bilal Shafi
  • 115
  • 2
  • 13
  • So... a long winded way to do Select(Func).ToArray()? I don't understand why you create ret, add the item one by one, then return it as array. Just create the array (the length of t is known anyway) and fill it. – Martheen May 29 '17 at 02:25
  • I just tried it, and you can replace the whole content of ArrayTo with : return t.Select(a).ToArray(); In fact it's probably more flexible for you to just use Select in the first place, if you don't need immediate execution it could be faster when you chain it to another process – Martheen May 29 '17 at 02:31
  • The thing is this works with all objects. That's what I was trying to do from the start. The code in my question was just an example. – Bilal Shafi May 29 '17 at 03:16
  • Um.. Select works on all IEnumerables (arrays, list, dictionaries, so on) of any types, and it accept any functions that accept the type, it doesn't even have to return the same type (eg, var listOString = arrayOfInts.Select(int.Parse).ToList() ), way more flexible – Martheen May 29 '17 at 09:49
  • Alright I just tested it and you're right. Now I'm no sure what to do. Since you answered this question can you put up that as an answer so I can accept it. – Bilal Shafi May 30 '17 at 23:27
  • Feel free to create another answer, relating how Select works with your use case. I gotta admit I'm still hazy on what you're trying to accomplish (like, if you need it to be immediately executed with ToArray/ToList, whether there might be different types in a single array, etc), so you're more capable to write the answer than me. – Martheen May 31 '17 at 03:12