private string[] ColeccionDeCortes(string Path)
{
DirectoryInfo X = new DirectoryInfo(Path);
FileInfo[] listaDeArchivos = X.GetFiles();
string[] Coleccion;
foreach (FileInfo FI in listaDeArchivos)
{
//Add the FI.Name to the Coleccion[] array,
}
return Coleccion;
}
Run Code Online (Sandbox Code Playgroud)
我想将其转换FI.Name为字符串,然后将其添加到我的数组中.我怎样才能做到这一点?
为什么禁止Extension Method用ref修饰符调用?
这个是可能的:
public static void Change(ref TestClass testClass, TestClass testClass2)
{
testClass = testClass2;
}
Run Code Online (Sandbox Code Playgroud)
这不是:
public static void ChangeWithExtensionMethod(this ref TestClass testClass, TestClass testClass2)
{
testClass = testClass2;
}
Run Code Online (Sandbox Code Playgroud)
但为什么?
所以基本上我Add为数组类型编写了我的小扩展方法。
using System;
using System.Linq;
public static class Extensions
{
public static void Add<T>(this T[] _self, T item)
{
_self = _self.Concat(new T[] { item }).ToArray();
}
}
public class Program
{
public static void Main()
{
string[] test = { "Hello" };
test = test.Concat(new string[] { "cruel" }).ToArray();
test.Add("but funny");
Console.WriteLine(String.Join(" ", test) + " world");
}
}
Run Code Online (Sandbox Code Playgroud)
输出应该是Hello cruel but funny world,但but funny永远不会在扩展方法中连接。
在扩展中编辑相同的数组似乎也不起作用:
using System;
using System.Linq;
public static class …Run Code Online (Sandbox Code Playgroud)