Creating and calling functions from Lists?

Hello,
I have a ‘scenario’ where I would like to create a function inside a List that would return specific data from the list. Here is an example…

public class C1 {
    public class C2  {
        public String S1;
        public String S2;
    }

    public List<C2> c2List = new List<C2>();

}

c2List is a list of data that has the S1 and S2 ‘properties’ (as Strings) per element, and I would like to create a function to where I can call…

c2List.getS1OnlyFromList();

straight from the list, and have something in the getS1OnlyFromList() function like…

public List<String> getS1OnlyFromList() {
    List<String> returnStringList = new List<String>();
    returnStringList.Clear();

    for (int i = 0; i < this.Count; i++) {
        returnStringList.Add(this[i].S1);
    }

    return returnStringList;
}

How would I set something up like this? Wouldn’t this be a good idea?

Thank you,
Michael S. Lowe

… or maybe a static function like…

//This function is declared as a 'static function' inside the C2 class' list.
public static List<String> getS1OnlyFromList(List<C2> c2List) {
    List<String> returnStringList = new List<String>();
    returnStringList.Clear();
    for (int i = 0; i < c2List.Count; i++) {
        returnStringList.Add(c2List[i].S1);
    }
    return returnStringList;
}

…and a function call like…

List<C2>.getS1OnlyFromList(c2List);

If you want to create extension methods you have to place them in a static class, make them static and pass them the extended class as this-ref, for example:

static class C2ListExtensions {
public static List<string> GetS1OnlyFromList(this List<C2> c2List) {
//your code to return the List with strings
}
}

Though i doubt that’s easier than just creating a function in C1 that returns the stringlist.

This is true.