1

I'm working with Json As i converted that json into classes and my class structure is like below

Class Demo
{
    public A a { get; set; }
    public B b { get; set; }
    public C[] c { get; set; }
}

class A
{
public string x{ get; set; }
}
class B
{
public string y{ get; set; }
}
class C
{
public string z{ get; set; }
public D d{ get; set; }
}
class D
{
public string p{ get; set; }

}

to assign values to the parameter i'm using below code.

 Demo demo = new Demo {

                        a = new A{
                            x = "test data",

                        },
                        b = new B{
                            y = "test data",

                        }

for the same class A and B i'm able to assign value but Class C is an array how can i assign parameter value to the class C

dev
  • 163
  • 1
  • 11
  • `c = new C[0]` ? – ColinM Aug 20 '19 at 11:17
  • 1
    c = new C[] { new C { z = "1"}, new C { z = "2"}} – jdweng Aug 20 '19 at 11:18
  • You can work with a list and use LINQ's `ToArray()` particularly if you're going to be reading those from a file/service or user input and cannot hard code it. `c = yourListOfC.ToArray()` – Fabulous Aug 20 '19 at 11:21
  • Possible duplicate of [Populate array directly through object initializer](https://stackoverflow.com/questions/26085930/populate-array-directly-through-object-initializer) – Heretic Monkey Aug 20 '19 at 11:28

2 Answers2

2

Two ways,

 Demo demo = new Demo
        {
            a = new A
            {
                x = "test data",
            },
            b = new B
            {
                y = "test data",
            },
            c = new C[]
            {
                new C
                {
                    z = "hello",
                    d = new D
                    {
                        p = "This is D's property"
                    }
                },
                new C
                {
                    z = "Hi",
                    d = new D
                    {
                        p = "Another D's property"
                    }

                },

            }
        };

Or add to list and convert to the array. I would recommend the first one.

 Demo demo = new Demo
        {
            a = new A
            {
                x = "test data",
            },
            b = new B
            {
                y = "test data",
            },
            c = new List<C>
            {
                new C
                {
                    z = "hello"
                }
            }.ToArray()
        };

Here is the full sample program,

class Program
{
    static void Main(string[] args)
    {
        Demo demo = new Demo
        {
            a = new A
            {
                x = "test data",
            },
            b = new B
            {
                y = "test data",
            },
            c = new C[]
           {
                new C
                {
                    z = "hello",
                    d = new D
                    {
                        p = "This is D's property"
                    }
                },
                new C
                {
                    z = "Hi",
                    d = new D
                    {
                        p = "Another D's property"
                    }

                },

           }
        };


        Console.WriteLine(demo.a.x);
        Console.WriteLine(demo.b.y);

        foreach (var c in demo.c)
        {
            Console.WriteLine(c.z);
            Console.WriteLine(c.d.p);
        }

        Console.ReadKey();
    }
}

public class Demo
{
    public A a { get; set; }
    public B b { get; set; }
    public C[] c { get; set; }

    public D d { get; set; }
}

public class A
{
    public string x { get; set; }
}

public class B
{
    public string y { get; set; }
}

public class C
{
    public string z { get; set; }
    public D d { get; set; }
}

public class D
{
    public string p { get; set; }

}

and the output,

enter image description here

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43
0

Create new empty array where each item would be of type C:

c = new C[0]

Create new array with two new items of type C, where each item has property values set:

c = new C[] { new C { z = "1"}, new C { z = "2"}}

However, if you don't know the number of items beforehand, you may be better of using generic enumerables, such as:

// Create new empty list
var myList = new List<C>();

// Add a new object to the list
myList.Add(new C());
Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72