.NET BCL中是否有采用`ref`参数的方法?

时间:2009-07-01 19:13:19

标签: c# parameters

TryParse方法系列使用out个参数;我可以给Swap作为ref参数有用的示例,但.NET基类库中有很好的例子吗?

2 个答案:

答案 0 :(得分:7)

mscorlib中有很多内容。运行此命令以查找它们,并在不同的程序集中为其显示其他类型。

using System;
using System.Linq;

class Test
{    
    static void Main()
    {
        ShowRefsInAssemblyContaining(typeof(string));
    }

    static void ShowRefsInAssemblyContaining(Type exampleType)
    {
        var query = from type in exampleType.Assembly.GetTypes()
                    where type.IsPublic
                    from method in type.GetMethods()
                    where method.GetParameters()
                                .Any(p => p.ParameterType.IsByRef &&
                                         !p.IsOut)
                    select method;

        foreach (var method in query)
        {
            Console.WriteLine(method.DeclaringType + ": " + method);
        }
    }
}

最简单的例子:Interlocked.CompareExchange

(难道你不喜欢LINQ,顺便说一句?)

答案 1 :(得分:0)

我相信你的意思是基类库......

如果是这样,有各种位置采用ref参数。一个例子是System.Threading中的Interlocked.Exchange