有人可以在OOP上下文中提供方法与函数的简单解释吗?
我正在学习C#中的指针,并且好奇是否可以在C#中使用C++样式函数指针.是的,我知道C#有自己的等效功能指针概念(称为委托).但我只是想知道使用C#中的指针是否可以实现相同,而不使用委托.
如果在C#中使用指针是完全合法的(使用不安全选项)并且指针语义几乎与C/C++类似,那么在我看来,也应该能够使用C/C++样式函数指针.请指导我这个.可能吗?如果是的话怎么样?如果不是那么为什么?
请注意C#和C/C++中指针使用的相似性,如下例所示
/* Using pointers in C# (Very similar to C/C++) */
using System;
namespace UnsafeCodeApplication
{
class TestPointer
{
public unsafe static void Main()
{
int[] list = {10, 100, 200};
fixed(int *ptr = list)
/* let us have array address in pointer */
for ( int i = 0; i < 3; i++)
{
Console.WriteLine("Address of list[{0}]={1}",i,(int)(ptr + i));
Console.WriteLine("Value of list[{0}]={1}", i, *(ptr + i));
}
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在Google上搜索方法和功能之间的区别,我得到了两个答案.
方法是如下所示的非返回类型
void Method()
{
}
Run Code Online (Sandbox Code Playgroud)
其中Function是一个返回类型,如下所示
int Method()
{
}
Run Code Online (Sandbox Code Playgroud)
这两个术语没有区别.
查询 - 哪个是正确的还是有任何第三件事?