C# 为什么我可以在静态主函数中使用类成员变量?
我知道,静态函数不允许C#中有任何静态变量,对吗?C# 为什么我可以在静态主函数中使用类成员变量?,c#,C#,我知道,静态函数不允许C#中有任何静态变量,对吗? Main()函数是静态函数,但我一直在使用自己的类 例如: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { public int si
Main()
函数是静态函数,但我一直在使用自己的类
例如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public int size = 300;
static void Main(string[] args)
{
Test t = new Test();
// this is error
size = 500;
// why this is not error, despite Test Class's object's member length is not a static member?
// i think that Main is static if so length variable shouldn't it be static ?
t.length = 300;
}
}
class Test
{
public int length;
}
}
静态变量在类的生命周期中仅初始化一次,而非静态变量的初始化次数为0或n,具体取决于为该类创建的对象数 在c#中处理静态和非静态成员时要遵循的规则:
请参阅此部分,以清楚了解C#中的静态和非静态成员。静态变量在类的生命周期中仅初始化一次,而非静态变量的初始化次数为0或n,具体取决于为该类创建的对象数 在c#中处理静态和非静态成员时要遵循的规则:
class Car
{
public int Wheels = 4;
}
Car myCar = new Car();
myCar.Wheels = 8;
Console.WriteLine(myCar.Wheels); //output: 8
另外请注意,当您尝试像类汽车
的静态
属性那样访问它时,它将不起作用:
class Car
{
public int Wheels = 4;
}
Car.Wheels = 8; //ERROR
静态类 与静态类/静态方法相比的区别在于,您只访问了一次(它是静态的!):
组合(你的例子) 每个类(静态或非静态)都可以有一个静态方法/变量。以下两项均有效:
class FirstClass
{
public static int x = 5;
public static void Test() { }
}
static class SecondClass
{
public static int x = 5;
public static void Test() { }
}
但不同之处在于,当你不想与他们合作时:
//Working with non-static class, is an instance, can create multiple instances
FirstClass fc = new FirstClass();
FirstClass fc2 = new FirstClass();
FirstClass fc3 = new FirstClass();
fc.x = 3;
Console.WriteLine(fc.x);
fc.Test();
//Static class has only single instance, created on program start - it is "static"
SecondClass.x = 3;
Console.WriteLine(SecondClass.x);
SecondClass.Test();
方法
static void Main()
是静态的,这意味着它只能访问其他静态属性/方法。要访问“非静态”属性/方法,您需要对已创建实例的引用,如new Program()
:
非静态类(普通类)
当您拥有一个类时,您必须创建它(实例化它),然后才能访问它的属性。例如:
class Car
{
public int Wheels = 4;
}
Car myCar = new Car();
myCar.Wheels = 8;
Console.WriteLine(myCar.Wheels); //output: 8
另外请注意,当您尝试像类汽车
的静态
属性那样访问它时,它将不起作用:
class Car
{
public int Wheels = 4;
}
Car.Wheels = 8; //ERROR
静态类 与静态类/静态方法相比的区别在于,您只访问了一次(它是静态的!):
组合(你的例子) 每个类(静态或非静态)都可以有一个静态方法/变量。以下两项均有效:
class FirstClass
{
public static int x = 5;
public static void Test() { }
}
static class SecondClass
{
public static int x = 5;
public static void Test() { }
}
但不同之处在于,当你不想与他们合作时:
//Working with non-static class, is an instance, can create multiple instances
FirstClass fc = new FirstClass();
FirstClass fc2 = new FirstClass();
FirstClass fc3 = new FirstClass();
fc.x = 3;
Console.WriteLine(fc.x);
fc.Test();
//Static class has only single instance, created on program start - it is "static"
SecondClass.x = 3;
Console.WriteLine(SecondClass.x);
SecondClass.Test();
方法
static void Main()
是静态的,这意味着它只能访问其他静态属性/方法。要访问“非静态”属性/方法,您需要对已创建实例的引用,如new Program()
:
请不要标记垃圾邮件。仅使用实际编写代码的语言标记。如果有此类的实例可用,也可以通过静态方法访问此类的非静态字段。这里有一个使用
newtest()
t.length=300创建的实例t
此中没有错误
,因为您使用t
访问它,但是size
您不能这样访问,您必须使用(新程序())。size=100代码>基本上是程序类的对象
。谢谢大家^^请不要标记垃圾邮件。仅使用实际编写代码的语言标记。如果有此类的实例可用,也可以通过静态方法访问此类的非静态字段。这里有一个使用newtest()
t.length=300创建的实例t
此中没有错误
,因为您使用t
访问它,但是size
您不能这样访问,您必须使用(新程序())。size=100代码>基本上是程序类的对象
。谢谢大家^^在fc.x=3代码>和fc.Test()代码>您正在使用字段/方法,就像它们是实例字段/方法一样。但是,它们是静态的,应该这样访问以避免混淆。IDE通常会对此类访问发出警告。将它们更改为static
access,或者将示例类FirstClass
更改为使用非静态字段/方法。但是,这可能会改变您试图说的示例的含义。@Progman我理解背后的原因,我的解释可能不好,请不要犹豫,编辑它以便更好地清除/解释。fc.x=3代码>和fc.Test()代码>您正在使用字段/方法,就像它们是实例字段/方法一样。但是,它们是静态的,应该这样访问以避免混淆。IDE通常会对此类访问发出警告。将它们更改为static
access,或者将示例类FirstClass
更改为使用非静态字段/方法。但是,这可能会改变您试图说的示例的含义。@Progman我理解背后的原因,我的解释可能不好,请不要犹豫,编辑它以供打赌