初始化 Struct 内的数组(字符串或任何其他数据类型)

发布于 2024-11-14 01:40:40 字数 485 浏览 3 评论 0原文

我想用 C# 来做这件事。

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

一般来说,如果需要,应该如何动态初始化数组? 简而言之,我试图在 C# 中实现这一点:

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

另一个例子:

  string[] array1;  
      for (int i=0; i < DynamicValue; i++) 
            array1[i] = "SomeValue";

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";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花ソ最美的依靠 2024-11-21 01:40:40

首先,您的代码几乎可以工作:

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

您可以通过添加自定义构造函数来初始化结构中的数组,然后在创建结构时调用构造函数来初始化它。这对于课堂来说是必需的。

话虽这么说,我强烈建议在这里使用类而不是结构。可变结构是一个坏主意 - 包含引用类型的结构也是一个非常坏的主意。


编辑:

如果您尝试创建长度动态的集合,则可以使用 List 而不是数组:

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]); 

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]); 
迟到的我 2024-11-21 01:40:40
int[] arr = Enumerable.Range(0, 10).ToArray();

更新

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

update

int x=10;
int[] arr = Enumerable.Range(0, x).ToArray();
倒带 2024-11-21 01:40:40
// 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;}
    }
}  
// 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;}
    }
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文