C#Func(T)不接受ref类型输入参数 [英] C# Func(T) not accepting ref type input parameter

查看:979
本文介绍了C#Func(T)不接受ref类型输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Func(T)接受C#中的引用类型变量。

Can Func(T) accepts the reference type variables in C#.

static void Main()
    {
        Func<string,int, int> method = Work;
        method.BeginInvoke("test",0, Done, method);
        // ...
        //
    }
    static int Work(ref string s,int a) { return s.Length; }
    static void Done(IAsyncResult cookie)
    {
        var target = (Func<string, int>)cookie.AsyncState;
        int result = target.EndInvoke(cookie);
        Console.WriteLine("String length is: " + result);
    }



我不能定义一个可以接受ref类型输入参数的func。任何人请启发...

I am not able define a func which can accept the ref type input parameter. Anybody please enlighten...

推荐答案

Func< T> 不能使用 ref 参数。

您需要创建自己的委托类型,它需要 ref 参数。

The Func<T> delegates cannot take ref parameters.
You need to create your own delegate type which takes ref parameters.

但是,你不应该首先使用 ref

However, you shouldn't be using ref here in the first place.

这篇关于C#Func(T)不接受ref类型输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆