Dynamic Array in C#

Dynamic array in C#

Take a look at Generic Lists.

Dynamic Array Filling in C#

Looks like outp is defined as a 1-element array (so, only outp[0] is valid). If i > 0, you are accessing outside the array boundaries, hence the exception. Same applies to SavePath.

Since you want to have an array of elements, you can either

  1. Declare outp/Savepath outside the while cycle, specifying the correct number of elements (once declared, arrays have fixed size)
  2. Declare them as a List rather than Array. A List can be accessed via the [] operator, but also supports the .Add method allowing it to grow.

You could go with something like

List<string> outp = new List<string>();
List<string> SavePath = new List<string>();

for (int i = 0; i < FileNameFromPath.Length; ++i)
{
outp.Add("CS_" + FileNameFromPath[i]);
SavePath.Add(DirOutputOlis + @"\" + outp[i]);
}

dynamic Array object

When you create an object like

var person = new
{
FirstName = "Test",
LastName = new List<Person>() { new Person()
{
FirstName = "Tes2"
} }
};

LastName is a generic list and Property.PropertyType.IsArray returns false on that case. So your "array/list" is not treated with this logic that you are trying to add

dynamic ArrayProperty = new List<dynamic>();
var ArrayElements = (Array)Property.GetValue(AnonymousObject);
for (var i = 0; i < ArrayElements.Length; i++) {
var Element = ArrayElements.GetValue(i);
if (IsPrimitive(Element.GetType())) {
ArrayProperty.Add(Element);
} else {
ArrayProperty.Add(ToExpando(Element));
}
}

Hope this helps

Just one remark, you don't need to check again inside the logic of the if(Property.Property.Type.IsArray) the Primitive values, you did it, that is one of the stop conditions of your recursion. Below is the same code with the difference that I am mentioning

 public static ExpandoObject ToExpando(this object AnonymousObject)
{
dynamic NewExpando = new ExpandoObject();
foreach (var Property in AnonymousObject.GetType().GetProperties())
{
dynamic Value;
if (IsPrimitive(Property.PropertyType))
{
Value = Property.GetValue(AnonymousObject);
}
else if (Property.PropertyType.IsArray)
{
var ArrayProperty = new List<ExpandoObject>();
var elements = Property.GetValue(AnonymousObject) as IEnumerable;

//is the same as foreach all elements calling to Expando and adding them to elemenstArray
if (elements != null)
ArrayProperty.AddRange(from object elem in elements select ToExpando(elem));

Value = ArrayProperty;
}
else
{
Value = ToExpando(Property.GetValue(AnonymousObject));
}
((IDictionary<string, object>)NewExpando)[Property.Name] = Value;
}
return NewExpando;
}

How to Create a Dynamic Array?

This can be easily done with a List.

Example:

List<string> words = new List<string>();
...
words.Add(Console.ReadLine());

Lists are dynamically expanding and you don't have to manage the size of the list on your own. The .NET Framework does that for you. You can also insert an item anywhere in the middle or delete one from any index.

Dynamically Create an Array in C#

You can also use the new operator just like with other object types:

int[] array = new int[5];

or, with a variable:

int[] array = new int[someLength];

C# Dynamic array

You may create a class and use List<MyClass>

class MyClass
{
public string Name {get;set;}
public double Price {get;set;}
public int Pages {get;set;}
}

Here is the list:

List<MyClass> values = new List<MyClass>();

Adding item

values.Add(new MyClass(){Name = "Book 1", Pages = 42, Price=50.0});

Insert at specific index:

values.Insert(0,new MyClass(){Name = "Book 2", Pages = 432, Price=10.0});

Retrieve at specific index:

MyClass theClass = values[1];

C# dynamic multidimensional list or array

This comes close to what you want

int[][] x =  { new[]{1, 2 }, new[]{ 3 }, new[]{ 4, 5, 6 }, new[]{ 7, 8 } };

All top level elements will be arrays, so x[1] is not 3 but an array with 1 element.

x[0][0] = 1;
x[1][0] = 3;
//Note: x[1][1] => 'x[1][1]' threw an exception of type 'System.IndexOutOfRangeException'

In your question you have

> Array (
> [0] => 1
> [1] => 2
> [2] => 3

but then you try to declare it{ { 1, 2 }, { 3 },.

You can achieve both with

int[][] x =  { new[]{1, 2 }, new[]{ 3 }, new[]{ 4, 5, 6 }, new[]{ 7, 8 } };

vs

int[][] x =  { new[]{1}, new[]{2}, new[]{ 3 }, new[]{ 4, 5, 6 }, new[]{ 7, 8 } };


Related Topics



Leave a reply



Submit