.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

How to make array of structure dynamic in C#

Hi guys i gave insufficient inform in my last post here is the description of my problem... one of my proj i have created the array of structure like the one below

 public struct profile
        {  public string username;
            public string name;
            public string userpassword;
            public string serverurl;
            public string port;
            public string auth;
            public string db;
            public string dbtype;
            public Int32 synctype;
            public bool chk;
        static profile[] profileset = new profile[100];

but i have to make this array dynamic at this point of time i cant change it to array list as it is tightly coupled with my project...  IS THERE ANY WAY TO DYNAMICALLY MAKE THE ARRAY GROW IN RUN TIME  in C#...please guys i badly require this information... i am using .net 2.0  waiting for u r answers..

Thanks and Regards,
Friday, January 12, 2007
Create a new array of the required size and copy it
Friday, January 12, 2007
> i cant change it to array list as it is tightly coupled with my project

I think you should.


No, but you can:

 Create a different array with a different size
 Copy elements from the old array into the new array
 Use the new array instead of the old array

Something like:

static void appendNewProfile(profile newProfile)
  profile[] newProfiles = new profile[profileset.Count+1];
  newProfiles[profileset.Count] = newProfile;
  profileset = newProfiles;

Beware through that if other places in your code have a reference to the old profileset, this operation won't replace those other references (you'll end up with two different references to two different arrays).
Christopher Wells Send private email
Friday, January 12, 2007
This'll translate between a list and an array transparently:

class Program
    static void Main(string[] args)
        string[] array = { "One", "Two", "Three" };
        ArrayList<string> list = array;
        Console.WriteLine(string.Join(", ", list));

    class ArrayList<T> : List<T>
        public static implicit operator T[](ArrayList<T> list)
            return list.ToArray();

        public static implicit operator ArrayList<T>(T[] array)
            ArrayList<T> list = new ArrayList<T>();
            return list;
Thom Lawrence Send private email
Friday, January 12, 2007
Hi Guys what about array.resize its working great!!!!
any comments of using it will really helpful...
Saturday, January 13, 2007
Looks like thats new in 2.0.  It does exactly what everyone above has said however, creates a new array of the specified size and coppies the contents of the orriginal.
Saturday, January 13, 2007
I don't get it.  If an array is resizable, then isn't it called "List"?

Sunday, January 14, 2007
Array.Resize is hideous.  I don't know what they were thinking when they added this.  Some of the problems with it:

* It's poorly named.  Despite being named Array.Resize, it doesn't resize the array.  It creates a new array and copies the elements from the old array to it.  It does this whether the new size is smaller or larger than the old size. 
* It's dangerous.  References to the original array other than the one passed to Array.Resize will still point to the original array.  It would be very easy to write code that unintentionally accesses the old array.  You're asking for bugs by using Array.Resize. 
* It's slow.  If the size passed to Array.Resize is different from the array's size, whether larger or smaller, it'll always allocate a new array and copy the elements from the old array.

Use List<> as I suggested in the other thread.
SomeBody Send private email
Sunday, January 14, 2007

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz