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; }
}
3
  • 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<Configuration> via whoever needs such a list. Commented Jan 4, 2016 at 10:37
  • Agree with @YuvalItzchakov or you could inherent from Collection<Configuration> if you need to provide additional functionality for the collection but i'd still recommend changing the class name. Commented Jan 4, 2016 at 10:45
  • Possible duplicate of Adding values to a C# array Commented Jan 4, 2016 at 11:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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}]}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.