簡體   English   中英

如何在C#中的函數中動態創建列表

[英]How to dynamically create a List in a function in C#

我有一個用c編寫的代碼。 我在main中有一個指向int的指針,並將其傳遞給一個函數。 此函數分配內存並填充數組,然后返回。 基本上看起來像這樣:

main()
{
  int* array;
  function(&array);
}

void function(int** array)
{
  int size = 25;
  *array = malloc(size);
  (*array)[0] = 42;
}

尺寸主要未知。 如何在C#中執行此操作? 我嘗試使用List,但無法使其正常工作。 我已經嘗試了List和ref List,它們都給Index超出了范圍。

編輯:

這很好

class Program
{
    static void function(List<int> array)
    {
        array.Add(42);
    }

    static void Main(string[] args)
    {
        List<int> array = new List<int>();
        function(array);
    }
}

還有這個

class Program
{
    static void function(out int[] array)
    {
        array = new int[25];
        array[0] = 42;
    }

    static void Main(string[] args)
    {
        int[] array;
        function(out array);
    }
}

但是以下引發異常

class Program
{
    static void function(out List<int> array)
    {
        array = new List<int>(25);
        array[0] = 42;
    }

    static void Main(string[] args)
    {
        List<int> array;
        function(out array);
    }
}
Class Demo
{


 static void main (string[] args)
 {
    var result=Add();
    Console.WriteLine(result[0]);
 }

  static List<int> Add ()
  {
    var listOfints= new List<int>();
    listOfints.Add(42); //either this or declare an array of integers and get it initialized by reading the user value and pass the same as param here to initialize and return the array
    return listOfints;
  }

}

為此, 具有ref關鍵字。 盡管在這種情況下並不需要,因為無論如何數組都是引用類型,但這是一個好主意,因為它傳達了設計意圖。 最好還是使用out關鍵字作為該函數的參數,而無需在調用之前初始化。

嘗試這個:

class Program
{
    static void Main(string[] args)
    {
        int[] a;
        function(out a);
        Debug.WriteLine(a.Length);
    }

    public static void function(out int[] array)
    {
        array=new int[25];
        array[0]=42;
    }
}

編輯1-返回數組的替代方法

class Program
{
    static void Main(string[] args)
    {
        int[] a = CreateArray();
        Debug.WriteLine(a.Length);
    }

    public static int[] CreateArray() 
    {
        int[] array=new int[25];
        array[0]=42;
        return array;
    }
}

編輯2-具有兩個out參數的示例

class Program
{
    static void Main(string[] args)
    {
        int[] akw, zuk;
        function(out akw, out zuk);
        Debug.WriteLine(akw.Length);
        Debug.WriteLine(zuk.Length);
    }

    public static void function(out int[] A, out int[] B) 
    {
        A=new int[25];
        B=new int[15];
        A[0]=42;
        B[0]=21;
    }
}

我認為您在將值[0]分配給ac#列表時遇到問題的原因是,除非您創建具有預定義起始大小的List <>集合,否則它將為空。 因此,條目[0]為空,無法設置。

如果要直接訪問元素0,請使用以下命令:

  void function(out List<int> list)
  {
     list = new List<int>(25);
     list[0] = 42;
  }

相反,如果您不需要/不知道列表的初始分配大小,請使用以下命令:(推薦的恕我直言)

  void function(out List<int> list)
  {
     list = new List<int>();
     list.Add(42);
  }

祝好運!

另一種方法(盡管建議使用List<int>

out int[]替換int *

public class Program
{
    public static void Main()
    {
        int[] array;
        function(out array);
        Console.WriteLine(array[0]);
    }

    static void function(out int[] array)
    {
        int size = 25;
        array = new int[size];
        array[0] = 42;
    }
}

輸出量

42

如上所述,c#使用引用。 支持指針,但是您需要編寫非托管代碼。
有關參考的更多信息,請參見此處https://msdn.microsoft.com/zh-cn/library/14akc2c7.aspx

下面是一個示例。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace alistnamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> my_list=new List<int>();
            functionaddtolist(ref my_list);
            for(int i=0;i<my_list.Count;i++)
            {
                Console.WriteLine("List item " + i.ToString() + "=" + my_list[i]);
            }
        }

        static void functionaddtolist(ref List<int> mylist_ref)
        {
            mylist_ref.Add(42);             //Add one element

        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM