【发布时间】:2016-10-25 03:36:25
【问题描述】:
我们知道委托是引用类型,指的是函数。
using System;
namespace Testing_Delegates
{
class Program
{
delegate void fun_delegate();
fun_delegate response;
static void Main(string[] args)
{
Program p = new Program();
p.fun();//Calling function directly
p.response += p.fun;//Setting reference to the function via delegate
}
public void fun()
{
int lvariable = 10000;
string rvariable = "Hello Developers!";
Console.WriteLine(lvariable + " " + rvariable);
}
}
}
我们知道函数 (fun) 内部的局部变量是 lvariable,它在堆栈上分配内存,而 rvariable 包含对存储字符串的堆的内存位置的引用。
现在,
- 当我们定义委托或委托实例时,它们(fun_delegate 和响应)在哪里分配内存,无论是在堆栈上还是在堆上?
- 为什么需要创建委托实例以将其引用到任何函数?
【问题讨论】:
-
可能你想查看这个帖子stackoverflow.com/questions/22431902/…
标签: c# .net memory-management reference delegates