2

I'm looking to do this in C#.

public struct Structure1
{ string string1 ;            //Can be set dynamically
  public string[] stringArr; //Needs to be set dynamically
}

In general, how should one initialize an array dynamically if need be? In simplest of terms, I'm trying to achieve this in C#:

  int[] array;  
  for (int i=0; i < 10; i++) 
        array[i] = i;  

Another example:

  string[] array1;  
      for (int i=0; i < DynamicValue; i++) 
            array1[i] = "SomeValue";
9
  • How do you mean dynamically? You don't want to pass those values to the constructor? Commented Jun 7, 2011 at 21:41
  • 1
    It's not clear to me where the struct part comes in. Why do you have a mutable struct in the first place? Commented Jun 7, 2011 at 21:42
  • Do you want to set int values to your string array? Commented Jun 7, 2011 at 21:46
  • By dynamically, I mean that the size of the array is unknown. So using int[] array = new int[10] or specifying 10 anywhere is not helpful.the array should get values at a later point in the code and it's size should also be determined at that time. Is it possible to utilize another data type in this case? Commented Jun 7, 2011 at 21:47
  • I want to use an array/string array etc. to be initialized later (not at declaration) to be used inside a struct Commented Jun 7, 2011 at 21:48

3 Answers 3

3

First off, your code will almost work:

int[] array = new int[10]; // This is the only line that needs changing  
for (int i=0; i < 10; i++) 
    array[i] = i; 

You could potentially initialize your arrays within your struct by adding a custom constructor, and then initialize it calling the constructor when you create the struct. This would be required with a class.

That being said, I'd strongly recommend using a class here and not a struct. Mutable structs are a bad idea - and structs containing reference types are also a very bad idea.


Edit:

If you're trying to make a collection where the length is dynamic, you can use List<T> instead of an array:

List<int> list = new List<int>();
for (int i=0; i < 10; i++) 
    list.Add(i);

// To show usage...
Console.WriteLine("List has {0} elements.  4th == {1}", list.Count, list[3]); 
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, what if I do not know the size of the array is 10 and it should be determined dynamically?
@Amy: Then you should consider using List<T> instead of an array.
I would need to pass this information into a Javascript call, which is why I was thinking in terms of an array or string to keep it simple. Any other ideas? Thank you!
1
int[] arr = Enumerable.Range(0, 10).ToArray();

update

int x=10;
int[] arr = Enumerable.Range(0, x).ToArray();

Comments

0
// IF you are going to use a struct
public struct Structure1
{
    readonly string String1;
    readonly string[] stringArr;
    readonly List<string> myList;

    public Structure1(string String1)
    {
        // all fields must be initialized or assigned in the 
        // constructor


        // readonly members can only be initialized or assigned
        // in the constructor
        this.String1 = String1

        // initialize stringArr - this will also make the array 
        // a fixed length array as it cannot be changed; however
        // the contents of each element can be changed
        stringArr = new string[] {};

        // if you use a List<string> instead of array, you can 
        // initialize myList and add items to it via a public setter
        myList = new List<string>();
    }

    public List<string> StructList
    {
        // you can alter the contents and size of the list
        get { return myList;}
    }
}  

Comments

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.