为什么不能使用流利的语言string?
例如:
var x = "asdf1234";
var y = new string(x.TakeWhile(char.IsLetter).ToArray());
Run Code Online (Sandbox Code Playgroud)
是不是有更好的转换IEnumerable<char>方式string?
这是我做的一个测试:
class Program
{
static string input = "asdf1234";
static void Main()
{
Console.WriteLine("1000 times:");
RunTest(1000, input);
Console.WriteLine("10000 times:");
RunTest(10000,input);
Console.WriteLine("100000 times:");
RunTest(100000, input);
Console.WriteLine("100000 times:");
RunTest(100000, "ffff57467");
Console.ReadKey();
}
static void RunTest( int times, string input)
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < times; i++)
{
string output = new string(input.TakeWhile(char.IsLetter).ToArray());
}
sw.Stop();
var …Run Code Online (Sandbox Code Playgroud) 每当我调试一段涉及数组或int,double,string等列表的代码时,我更喜欢有时打印它们.我为此做的是为不同类型编写重载的printArray/printList方法.
例如
我可以使用这3种方法来打印各种类型的数组
public void printArray(int[] a);
public void printArray(float[] b);
public void printArray(String[] s);
Run Code Online (Sandbox Code Playgroud)
虽然这对我有用,但我仍然想知道是否可以使用通用方法打印任何类型的数组/列表.这也可以扩展到对象的数组/列表.