如何在 C# 的函数中动态创建一个列表
How to dynamically create a List in a function in C#
我有一个用 c 语言编写的代码。我在 main 中有一个指向 int 的指针,并将它传递给一个函数。此函数分配内存并填充数组,然后 returns。基本上是这样的:
main()
{
int* array;
function(&array);
}
void function(int** array)
{
int size = 25;
*array = malloc(size);
(*array)[0] = 42;
}
main 中的大小未知。我如何在 C# 中执行此操作?我尝试使用 List,但无法使其正常工作。我已经尝试了 List 和 ref List,它们都给出了 Index was out of range.
编辑:
这个很好用
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;
}
}
c# has the 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] 分配给 c# 列表时遇到问题的原因是,除非您创建具有预定义起始大小的 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>
)
将int *
替换为out 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/en-us/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
}
}
}
我有一个用 c 语言编写的代码。我在 main 中有一个指向 int 的指针,并将它传递给一个函数。此函数分配内存并填充数组,然后 returns。基本上是这样的:
main()
{
int* array;
function(&array);
}
void function(int** array)
{
int size = 25;
*array = malloc(size);
(*array)[0] = 42;
}
main 中的大小未知。我如何在 C# 中执行此操作?我尝试使用 List,但无法使其正常工作。我已经尝试了 List 和 ref List,它们都给出了 Index was out of range.
编辑:
这个很好用
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;
}
}
c# has the 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] 分配给 c# 列表时遇到问题的原因是,除非您创建具有预定义起始大小的 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>
)
将int *
替换为out 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/en-us/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
}
}
}