Add property to list?

I have a list with a lot of objects List<MyObjects>- Iterate through this list and read objects. All perfectly. I just redid it now, that would be awesome if I could add 1 more special value to this list - not an object, but only 1 value of something (string).

Now I could create a class like

class Needless
{
  public List<MyObjects> MyList { get; set; }
  public string MyValue { get; set; }
}

but Im trying to avoid that. I just need 1 line with each input List<MyObjects>Any ideas?

+5
source share
3 answers

Tuple<string, List<MyObject>>is an option. However, if you intend to use this pairing a lot, I would advise you to create a custom class so that it is more explicit - either as you did, or output List<MyObject>and add the line as a property.

" ", :

var t = new { TheString = "", TheList = new List<MyObject>() };
var list = t.TheList;
var s = t.TheString;

. IntelliSense , .

, , ExpandoObject System.Dynamic:

var expando = new ExpandoObject();
expando.List = new List<MyObject>();
expando.TheString = "";

IntelliSense DLR. ExpandoObject IDictionary<string, object> , ...

var dict = (IDictionary<string, object>)expando;

....

- . , . , ExpandoObject , , . , , , , ...

+8

, ,

+2

You can extend the List implementation with the Needless class. This way you can treat the list as a list.

0
source

All Articles