-3

How to add/append some objects into an object array with C#, I can add a static object but I don't know how to add an object with dynamic.
please do me a favor to give me any slight clue to deal with this issue, thanks

   object dataSource = new object[] {
      new { a1 = "111",    a11 = "Simpson" },
      new { a1 = "Marge ", a11 = "Simpson" },
      new { a1 = "Bart",   a11 = "Simpson" },
      new { a1 = "Lisa",   a11 = "Simpson" },
      new { a1 = "Maggie", a11 = "Simpson" },
      ...............
      };

reference sample code

Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
  • 2
    It also seems you are following JavaScript style. I strongly recommend not to do this. C# is a statically typed language, and you are supposed to take advantage of it. Create class `SomeClass` with fields `a1` and `a11`, declare `dataSource` as `SomeClass[]` (or `List` if this approach is correct). –  Jun 03 '19 at 02:39
  • @dyukha I'd added a reference url – Willie Cheng Jun 03 '19 at 02:48
  • What dyukha means, you shouldn't use anonymous types, if you do not realy need them. Instead use compiler save strong types (e.g. specific class). Everything else in answered in my duplicate. – Christian Gollhardt Jun 03 '19 at 02:52
  • @mjwills and ChristianGollhardt, I've found a lot of references from SO, but I didn't see too many references about the pure object type so that I create this issue – Willie Cheng Jun 03 '19 at 03:02
  • 1
    Hence, you use anonymous types only for realy simple scenarios. Everything complex should have a strong type. – Christian Gollhardt Jun 03 '19 at 03:03
  • @ChristianGollhardt thanks for you recommend, I didn't see any very clear references in SO about Object(Anonymous) type, so that do I need to delete this question if it is not a good question,thanks – Willie Cheng Jun 03 '19 at 03:10
  • 1
    @WillieCheng There isn't a question re: object arrays, or arrays of anonymous types, specifically because there doesn't need to be. The duplicate answers apply equally to all types. – mjwills Jun 03 '19 at 03:14
  • @mjwills thanks for you recommend, I have tried to delete this issue, but I cannot I don't know why. – Willie Cheng Jun 03 '19 at 03:19
  • Probably because you got a [good answer (score >1)](https://meta.stackexchange.com/a/42297/292385), because we didn't closed the question fast enough. – Christian Gollhardt Jun 03 '19 at 03:21

1 Answers1

2

You can resize an array using Array.Resize(ref array, int size) but it can be difficult to manage. An easy option is to use a list that will manage the collection for you and when you finish it, convert it to array. For sample:

// define a list
var dataSourceList = new List<object>();

// add items
dataSourceList.Add(new { a1 = "111", a11 = "Simpson" });
dataSourceList.Add(new { a1 = "Marge ", a11 = "Simpson" });
dataSourceList.Add(new { a1 = "Bart", a11 = "Simpson" });
dataSourceList.Add(new { a1 = "Lisa", a11 = "Simpson" });
dataSourceList.Add(new { a1 = "Maggie", a11 = "Simpson" });
...

// convert it to array
var dataSource = dataSourceList.ToArray();
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
  • Thanks for your recommendation, how can I do if I want to add itmes(a1,a11,a111.....) by dynamic, thanks – Willie Cheng Jun 03 '19 at 06:20
  • There is a method on the list called `AddRange` that accepts a collection to be added on the list, but I'm not sure if it can accept in the `params` sintaxe. Anyway, you could implement an extension method to do this. – Felipe Oriani Jun 03 '19 at 11:37