Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,023 questions

51,974 answers

573 users

How to convert an ArrayList to an array of structures in C#

1 Answer

0 votes
using System;
using System.Collections;

public struct MyStruct
{
    public int Id;
    public string Name;

    public MyStruct(int id, string name) {
        Id = id;
        Name = name;
    }
}

public class Program
{
    public static void Main()
    {
        ArrayList arrayList = new ArrayList();
        
        arrayList.Add(new MyStruct(1, "aaa"));
        arrayList.Add(new MyStruct(2, "bbb"));
        arrayList.Add(new MyStruct(3, "ccc"));
        arrayList.Add(new MyStruct(4, "ddd"));

        // Convert ArrayList to an array of structures
        MyStruct[] structArray = (MyStruct[])arrayList.ToArray(typeof(MyStruct));

        foreach (var item in structArray) {
            Console.WriteLine($"Id: {item.Id}, Name: {item.Name}");
        }
    }
}

  
/*
run:
  
Id: 1, Name: aaa
Id: 2, Name: bbb
Id: 3, Name: ccc
Id: 4, Name: ddd
  
*/

 



answered Feb 9, 2025 by avibootz

Related questions

1 answer 85 views
1 answer 169 views
169 views asked Jan 28, 2019 by avibootz
1 answer 147 views
147 views asked Sep 10, 2020 by avibootz
1 answer 140 views
2 answers 236 views
236 views asked Feb 9, 2017 by avibootz
...