C# winform: Accessing public properties from other forms & difference between static and public properties

前端 未结 5 717
说谎
说谎 2020-12-03 18:54

I am trying to understand whats the difference between a static and public properties. But when I tried to access my public property \'Test\' in other form it says \'null\'.

5条回答
  •  我在风中等你
    2020-12-03 19:38

    public

    The type or member can be accessed by any other code in the same assembly or another assembly that references it.

    static

    The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.

    A static class is basically the same as a non-static class, but there is one difference: a static class cannot be externally instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

    However, there is a such thing as a static constructor. Any class can have one of these, including static classes. They cannot be called directly & cannot have parameters (other than any type parameters on the class itself). A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. Looks like this:

    static class Foo() { static Foo() { Bar = "fubar"; }

    public static string Bar { get; set; }
    

    }

    Static classes are often used as services, you can use them like so:

    MyStaticClass.ServiceMethod(...);

提交回复
热议问题