C#方法中的return 0 有什么作用?请各位大虾指点~~~~

如下代码中 非静态方法 InstanceMethod() 和静态方法 StaticMethod()中的return 0 ;有什么作用?是不是想C语言那样退出Main函数?如果是的话,为什么可以连续调用两次方法呢?d();如果不是,那return有什么作用呢?
using System;
delegate int MyDelegate(); //声明一个代表
public class MyClass
{
public int InstanceMethod()
{
Console.WriteLine("Call the instance method.");
return 0;
}
static public int StaticMethod()
{
Console.WriteLine("Call the static method.");
return 0;
}
}
public class Test
{
static public void Main()
{
MyClass p=new MyClass();
//将代表指向非静态的方法 InstanceMethod
MyDelegate d=new MyDelegate(p.InstanceMethod);
//调用非静态方法
d();
//将代表指向静态的方法StaticMethod
d=new MyDelegate(MyClass.StaticMethod);
//调用静态方法
d();
}
}
程序的输出结果是:call the instance method.call the static method.

其实这个返回值没起什么作用,只是方法定义了返回类型为int才需要返回0。其实完全可以定义成返回类型为void,就不需要返回值了。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-27
你想多了!return 0当然和c中一样
相似回答
大家正在搜