如何防止在循环中创建的对象在循环迭代结束时被释放

How can I prevent objects created in a loop from being released at the end of that iteration of the loop

如果我正在创建一个对象并且我希望它只持续当前代码块,之后它将被自动销毁(或至少标记为销毁),我可以使用 scope关键词:

using System;

class Program
{
    public void Program()
    {
        let basicString = String("Basic String");

        for (let i = 0 ; i < 4 ; i++)
        {
            let modifiedString = scope String(s);

            if (i%2 == 0)
            {
                modifiedString.ToUpper();
            }
            else
            {
                modifiedString.ToLower();
            }

            Console.WriteLine(modifiedString);

            // modifiedString is marked for destruction 
        }
    }
}

但是,如果我需要对象保留到块之后,例如整个方法或创建它的块之外的块,我如何指定该范围?例如,如何确保字符串存在于下面的第二个循环中?

using System;
using System.Collections.Generic;

class Program
{
    public void Program()
    {
        let basicString = String("Basic String");

        let modifiedStringList = scope List<String>();

        for (let i = 0 ; i < 4 ; i++)
        {
            let modifiedString = scope String(s);

            if (i%2 == 0)
            {
                modifiedString.ToUpper();
            }
            else
            {
                modifiedString.ToLower();
            }

            modifiedStringList.Append(modifiedString);
            Console.WriteLine(modifiedString);

            // somehow prevent modifiedString from being marked for destruction
        }

        modifiedStringList.RemoveAt(1);

        for (let s in modifiedStringList)
        {
            Console.WriteLine(s);
        }

        // allow all of the modifiedString to be marked for destruction here
    }
}

您可以使用 scope:: 不让整个方法的变量被标记为销毁:

using System;
using System.Collections.Generic;

class Program
{
    public void Program()
    {
        let basicString = String("Basic String");

        let modifiedStringList = scope List<String>();

        for (let i = 0 ; i < 4 ; i++)
        {
            // modifiedString won't be destroyed until after Program() exits.
            let modifiedString = scope:: String(s); 

            if (i%2 == 0)
            {
                modifiedString.ToUpper();
            }
            else
            {
                modifiedString.ToLower();
            }

            modifiedStringList.Append(modifiedString);
            Console.WriteLine(modifiedString);
        }

        modifiedStringList.RemoveAt(1);

        for (let s in modifiedStringList)
        {
            Console.WriteLine(s);
        }
    }
}

如果需要指定方法和当前块之间的任意范围,可以使用带有 BlockName: { ... } 的命名块并使用 scope:BlockName:

using System;
using System.Collections.Generic;

class Program
{
    public void Program()
    {
        for (let repeat=0; repeat<10; repeat++)
        RepeatBlock:                
        {
            let basicString = String("Basic String");

            let modifiedStringList = scope List<String>();

            for (let i = 0 ; i < 4 ; i++)
            {
                // modifiedString won't be destroyed until after 
                // the block named RepeatBlock block exits.
                let modifiedString = scope:RepeatBlock String(s); 

                if (i%2 == 0)
                {
                    modifiedString.ToUpper();
                }
                else
                {
                    modifiedString.ToLower();
                }

                modifiedStringList.Append(modifiedString);
                Console.WriteLine(modifiedString);
            }

            modifiedStringList.RemoveAt(1);

            for (let s in modifiedStringList)
            {
                Console.WriteLine(s);
            }
        }
    }
}