-5

I have a list of classes.

For Example:

public class Object()
{
    public string a;
    public string b;
}

public List<Object> Objects = new list<Object>();

What I want is a Method that can set the string a in each Object in the List to Object[0].a;

And I want to do this to b and a lot of other vars too with a single Method. (Other var types to, with only one Method) is this possible?

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188

1 Answers1

-1

Let's say you want a list of ten of those Object (unfortunately named) class instances in a list, and you want them all to have the a property set to "Foo". You can use Enumerable.Range to create an enumeration with ten elements, then Select to create the objects. When creating the object, you can initialize the a property using initializer syntax. Once they are created you can create the list with ToList.

Technically this is one line of code although for readability it may be a good idea to span it across a few visual lines.

List<Object> myList = Enumerable
    .Range(0,10)
    .Select
    (
        i => new Object { a = "Foo" }
    )
    .ToList();
John Wu
  • 50,556
  • 8
  • 44
  • 80