C# “你的”是什么;“啊哈时刻”;在理解代表方面?

C# “你的”是什么;“啊哈时刻”;在理解代表方面?,c#,performance,delegates,unity3d,C#,Performance,Delegates,Unity3d,考虑到在C#中使用委托,是否有人知道它是否有性能优势,或者对程序员来说是否方便?如果我们正在创建一个包含一个方法的对象,听起来好像该对象将是要调用的内存中的常量,而不是每次调用时都加载该方法。例如,如果我们查看以下基于Unity3D的代码: public delegate H MixedTypeDelegate<G, H>(G g) public class MainParent : MonoBehaviour // Most Unity classes inherit from

考虑到在C#中使用委托,是否有人知道它是否有性能优势,或者对程序员来说是否方便?如果我们正在创建一个包含一个方法的对象,听起来好像该对象将是要调用的内存中的常量,而不是每次调用时都加载该方法。例如,如果我们查看以下基于Unity3D的代码:

public delegate H MixedTypeDelegate<G, H>(G g)

public class MainParent : MonoBehaviour // Most Unity classes inherit from M.B.
{
    public static Vector3 getPosition(GameObject g)
    {
        /* GameObject is a Unity class, and Vector3 is a struct from M.B.
        The "position" component of a GameObject is a Vector3. This method
        takes the GameObject I pass to it & returns its position. */

        return g.transform.position;
    }

    public static MixedTypeDelegate<GameObject, Vector3> PositionOf;

    void Awake( ) // Awake is the first method called in Unity, always.
    {
        PositionOf = MixedTypeDelegate<GameObject, Vector3>(getPosition);
    }
}

public class GameScript : MainParent
{
    GameObject g = new GameObject( );
    Vector3 whereAmI;

    void Update( )
    {
        // Now I can say:
        whereAmI = PositionOf(g);

        // Instead of:
        whereAmI = getPosition(g);
    }
}
公共委托H混合类型委托(G)
公共类MainParent:monobhavior//大多数Unity类继承自M.B。
{
公共静态向量3 getPosition(游戏对象g)
{
/*GameObject是一个统一类,Vector3是M.B.中的一个结构。
游戏对象的“位置”部分是一个矢量3。这种方法
获取我传递给它的游戏对象并返回其位置*/
返回g.transform.position;
}
公共静态混合类型的代理位置;
void Awake()//Awake始终是Unity中调用的第一个方法。
{
PositionOf=混合类型委托(getPosition);
}
}
公共类游戏脚本:MainParent
{
GameObject g=新的GameObject();
向量3;
无效更新()
{
//现在我可以说:
式中mi=位置(g);
//而不是:
其中mi=获得位置(g);
}
}
。但这似乎是一个额外的步骤,除非有额外的小东西,它有帮助


我想问第二个问题最简洁的方式是这样说:当你了解代表时,背景/场景/来源是什么?

我的代表目的的aha时刻是事件。将委托添加到事件中,每个委托指向一个函数,该函数将以自己的方式处理委托,这种能力对我来说是有意义的。

委托的意义在于事件。将委托添加到事件中,并且每个委托都指向一个函数,该函数将以自己的方式处理该事件,这种能力对我来说很有意义。

委托的目的是在代码中提供一个运行时分支点

class Foo
{
    public void Method1()
    {
    }

    public SomeDelegateType Method2;
}
Method1是一个固定的代码点,调用它将始终导致执行相同的代码(Method1的主体)。方法2是一个委托。您可以将委托视为“指向零个或多个方法的指针”。分配给委托的每个方法都将按其添加的顺序调用。调用它的结果可以在运行时更改,因为可以随意添加/删除方法

将委托视为“变量方法”的另一种方式。您可以使用变量来保存可以更改的值。您使用委托来保存可以更改的方法“address(es)”。我在这里随意使用“地址”一词来保持答案的简单

已添加

对于您的评论,如果我理解正确,您希望知道如何指定委托类型

必须定义委托类型,就像任何其他类型一样:

// define a delegate type that returns 'float' and accepts zero parameters
delegate float AMethodThatReturnsSingle();

// here is a method that accepts one of those as a parameter
float Method(AMethodThatReturnsSingle d)
{
    // call the passed in delegate and return its value
    return d();
}
Net Framework提供了许多对常见情况有用的委托类型。下面是我们上面声明的自定义委托的替代品,该委托使用泛型
Func
委托类型:

float Method(Func<float> d)
{
    // call the passed in delegate and return its value
    return d();
}
float方法(Func d)
{
//调用传入的委托并返回其值
返回d();
}
Func有几种风格可支持参数:

// Encapsulates a method that has no parameters and returns a value of the type
//     specified by the TResult parameter.
delegate TResult Func<TResult>();
// Encapsulates a method that has one parameter and returns a value of the type
//     specified by the TResult parameter.
delegate TResult Func<T, TResult>(T arg);
// Encapsulates a method that has two parameters and returns a value of the type
//     specified by the TResult parameter.
delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
//封装一个没有参数的方法,并返回该类型的值
//由TResult参数指定。
委托TResult Func();
//封装具有一个参数并返回类型为的值的方法
//由TResult参数指定。
委托TResult Func(T参数);
//封装具有两个参数并返回类型为的值的方法
//由TResult参数指定。
委托TResult Func(T1 arg1,T2 arg2);
还有
Action
,它不返回任何内容,只接受指定类型的零个或多个参数(我相信最多定义了四个)

有一个
谓词
,它返回bool并接受指定类型的一个或多个参数

还有无处不在的
EventHandler
,这是推荐用于事件的类型


仅举几个例子。

委托的目的是在代码中提供运行时分支点

class Foo
{
    public void Method1()
    {
    }

    public SomeDelegateType Method2;
}
Method1是一个固定的代码点,调用它将始终导致执行相同的代码(Method1的主体)。方法2是一个委托。您可以将委托视为“指向零个或多个方法的指针”。分配给委托的每个方法都将按其添加的顺序调用。调用它的结果可以在运行时更改,因为可以随意添加/删除方法

将委托视为“变量方法”的另一种方式。您可以使用变量来保存可以更改的值。您使用委托来保存可以更改的方法“address(es)”。我在这里随意使用“地址”一词来保持答案的简单

已添加

对于您的评论,如果我理解正确,您希望知道如何指定委托类型

必须定义委托类型,就像任何其他类型一样:

// define a delegate type that returns 'float' and accepts zero parameters
delegate float AMethodThatReturnsSingle();

// here is a method that accepts one of those as a parameter
float Method(AMethodThatReturnsSingle d)
{
    // call the passed in delegate and return its value
    return d();
}
Net Framework提供了许多对常见情况有用的委托类型。下面是我们上面声明的自定义委托的替代品,该委托使用泛型
Func
委托类型:

float Method(Func<float> d)
{
    // call the passed in delegate and return its value
    return d();
}
float方法(Func d)
{
//调用传入的委托并返回其值
返回d();
}
Func有几种风格可支持参数:

// Encapsulates a method that has no parameters and returns a value of the type
//     specified by the TResult parameter.
delegate TResult Func<TResult>();
// Encapsulates a method that has one parameter and returns a value of the type
//     specified by the TResult parameter.
delegate TResult Func<T, TResult>(T arg);
// Encapsulates a method that has two parameters and returns a value of the type
//     specified by the TResult parameter.
delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
//封装一个没有参数的方法,并返回该类型的值
//由TResult参数指定。
委托TResult Func();
//封装具有一个参数并返回类型为的值的方法
//由TResult参数指定。
委托TResult Func(T参数);
//封装具有两个参数并返回类型为的值的方法
//由TResult参数指定。
委托TResult Func(T1