Interesting “params of ref” feature, any workarounds?

前端 未结 5 1224
轮回少年
轮回少年 2020-11-27 19:38

I wonder if there\'s any way something like this would be possible for value types...

public static class ExtensionMethods {
    public static void SetTo(thi         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 20:17

    Here is some interesting solution:

    public delegate RecursionRefFunc RecursionRefFunc(ref T arg);
    
    public static RecursionRefFunc Boo(ref T input)
    {
        Console.WriteLine(input); // Work in here
        return Boo;
    }
    
    public static void Main(string[] args)
    {
        int x1 = 1, x2 = 2, x3 = 3, x4 = 4, x5 = 5;
        Boo(ref x1)(ref x2)(ref x3)(ref x4)(ref x5);
    }
    
    // Output: //
    // 1
    // 2
    // 3
    // 4
    // 5
    

    Delegate can declare in recursion.

    Return a function outside and call again.

    And you will be killed by the code reviewer.


    Advertisement OW<: CWKSC/MyLib_Csharp

提交回复
热议问题