1

I have a Array Configuration[] wrapped with ConfigurationArray class, how I can add values to array using ConfigurationArray.

public class ConfigurationArray
{
    [DataMember(Name = "configurations")]
    public Configuration[] Configurations { get; set; }
}

public class Configuration
{        
    [DataMember(Name = "configurationType")]
    public string ConfigurationType { get; set; }

    [DataMember(Name = "configurationValue")]
    public string ConfigurationValue { get; set; }
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
iVikashJha
  • 159
  • 1
  • 2
  • 14
  • 1
    Having a `ConfigurationArray` class in which itself exposes a `Configuration` array feels a little weird to me. Instead of wrapping the array, I'd simply expose a `List` via whoever needs such a list. – Yuval Itzchakov Jan 04 '16 at 10:37
  • Agree with @YuvalItzchakov or you could inherent from `Collection` if you need to provide additional functionality for the collection but i'd still recommend changing the class name. – DGibbs Jan 04 '16 at 10:45
  • Possible duplicate of [Adding values to a C# array](http://stackoverflow.com/questions/202813/adding-values-to-a-c-sharp-array) – Sinatr Jan 04 '16 at 11:24

1 Answers1

8

You can't. An array has a fixed size, so the only thing you can do in order to add one to the array, is to 'expand the array' by creating a new one with a bigger size than the previous one and copy all items over.

The best option you have is to use a List<Configuration> instead of Configuration[], which is capable of sizing itself.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • I have this below json what type we can use this one in c#{"configurations":[{"configurationType":"CPC","configurationValue":2501},{"configurationType":"WLK","configurationValue":false},{"configurationType":"RDM","configurationValue":100},{"configurationType":"BCK","configurationValue":20}]} – iVikashJha Jan 04 '16 at 11:59