小编Pal*_*lus的帖子

将列表转换为数字范围字符串

这个问题几乎与这个问题相反: C#是否内置支持解析页码字符串?

所以给定

1,3,5,6,7,8,9,10,12:
Run Code Online (Sandbox Code Playgroud)

我会输出:

1,3,5-10,12
Run Code Online (Sandbox Code Playgroud)

这是我的第一次尝试.它看起来有点像hacky,可能是我写过的最糟糕的代码.你能提出一个改进的方法吗?更好的办法吗?

static string numListToRangeStr(List<int> numList)
{
    StringBuilder retString = new StringBuilder();
    numList.Sort();

    bool inRangeFind = false;
    int firstInRange = numList[0];
    int lastNumber = firstInRange;
    bool first = true;

    for (int i = 1; i < numList.Count; i++)
    {
        if (numList[i] == (lastNumber + 1))
        {
            inRangeFind = true;
        }
        else
        {             
            if (inRangeFind)
            {
                if (!first)
                {
                    retString.Append(",");
                }
                retString.Append(firstInRange);
                retString.Append("-");
            }
            else
            {
               if (!first)
                {
                    retString.Append(",");
                }
            }

            retString.Append(lastNumber);

            firstInRange = numList[i]; …
Run Code Online (Sandbox Code Playgroud)

c# string parsing

13
推荐指数
3
解决办法
4304
查看次数

将List传递给方法而不修改原始列表

这是将List传递给方法并编辑该List而不修改原始List的唯一方法吗?

class CopyTest1
{
    List<int> _myList = new List<int>();
    public CopyTest1(List<int> l)
    {
        foreach (int num in l)
        {
            _myList.Add(num);
        }
        _myList.RemoveAt(0); // no effect on original List
    }
}
Run Code Online (Sandbox Code Playgroud)

c# list pass-by-reference

5
推荐指数
2
解决办法
1万
查看次数

从C++启动C#.Net应用程序

是否可以从用C++编写的应用程序中启动C#.Net(2.0)应用程序?

谢谢,

编辑:很酷 - 所以我只需知道应用程序的位置:

LPTSTR szCmdline = _tcsdup(TEXT("C:\\Program Files\\MyApp -L -S"));
CreateProcess(NULL, szCmdline, /* ... */);
Run Code Online (Sandbox Code Playgroud)

c# c++ c#-2.0

2
推荐指数
1
解决办法
682
查看次数

标签 统计

c# ×3

c#-2.0 ×1

c++ ×1

list ×1

parsing ×1

pass-by-reference ×1

string ×1