方法在哪些条件下是必须要声明为static
以下代码为何要将function设置为static
如果不,那回有这样提示:
e:\c_sharp_master\hellocsharp\hellocsharp.cs\class1.cs(19,4): error CS0120: 非静态的字段、方法或属性“HelloCSharp.cs.HelloClass.function(int)”要求对象引用
本来以为引用对象调方法时候要用static,后来我用int ,她也这样提示.
不知道static声明条件........................
using System;
namespace HelloCSharp.cs
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class HelloClass
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
int x = 0;
System.Console.WriteLine( "Before entering function,it is {0}.", x );
function( x );
System.Console.WriteLine( "After entering function,it is {0}.", x );
}
public void function( int x )
{
x++;
}
}
}