繁体   English   中英

为什么我可以在static Main Function中使用class成员变量?

[英]Why can I use the class member variables in static Main Function?

据我所知,static function 不允许 C# 中有任何 static 变量,对吗? Main() function 是static function ,但是我一直在用自己的 class。

例子:

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;  
    }
}

static 变量在 class 的生命周期中仅被初始化一次,而非静态变量被初始化 0 次或 n 次,具体取决于为该 class 创建的对象数量。

使用 static 和 c# 中的非静态成员时要遵循的规则:

  1. 非静态到static:只能使用那个class的object来消费。
  2. Static至static:可直接消费或使用class名称消费。
  3. Static 到非静态:可以直接使用或使用 class 名称使用。
  4. Non-static to non-static:可以直接使用,也可以使用“this”关键字使用。

参考这个链接可以清楚的了解Static和C#中的非静态成员。

非静态类(普通类)

当你有一个 class 时,你必须创建它(实例化它),然后你可以访问它的属性。 例如:

class Car
{
    public int Wheels = 4;
}

Car myCar = new Car();
myCar.Wheels = 8;
Console.WriteLine(myCar.Wheels); //output: 8

另请注意,当您尝试像 class Carstatic属性一样访问它时,它不会工作:

class Car
{
    public int Wheels = 4;
}

Car.Wheels = 8; //ERROR

Static 班

与 static 类/静态方法相比的不同之处在于,您正在访问仅存在一次的内容(它是 static:):

static class MyStaticClass
{
    //public int y = 3;  //ERROR: this is invalid, static class can't have instance variables

    public static int x = 5;  //variable must be also static
}

//You don't have to instantiate (create `new MyStaticClass()`) anymore.
MyStaticClass.x = 3;
Console.WriteLine(MyStaticClass.x = 3);

组合(你的例子)

每个 class(static 或非静态)都可以有一个static 方法/变量 以下两项均有效:

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()static ,这意味着它只能访问其他 static 属性/方法。 要访问“非静态”属性/方法,您需要引用已创建的实例,例如new Program()

class Program
{
    public int size = 300;

    static void Main(string[] args)
    {
        size = 123; //Error, because it doesn't know to which instance it goes
        Program.size = 123; //Error, because Program is non-static class
        
        //It must first have some instance of Program to work with.
        Program p1 = new Program();
        Program p2 = new Program();
        p1.size = 123;
        p2.size = 234;
        Console.WriteLine(p1.size == p2.size); //compare, not same, 2 different properties
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM