简体   繁体   English

c#如何将静态类分配给实例变量?

[英]c# how assign static class to instance variable?

Static members can't be called with instance, like instance.myStaticProperty .不能使用实例调用静态成员,例如instance.myStaticProperty

Is there any way, that I can have an instance variable that will be an alias of static self class?有什么办法,我可以有一个实例变量,它将是静态自类的别名? like:喜欢:

class myClass
{
    public string a ="hello";
    public static string b ="world";

    public myClass myVariable = global::myClass;   // <--- phseudo code
}

and i could call:我可以打电话:

myClass instance= new myClass();
instance.myVariable.b; //

No, there is not.不,那里没有。 The closest you get is using a using .最接近的是使用using

Your static class definition:您的静态类定义:

class ClassA
{
    public static string A = "A";
}

And to use it:并使用它:

using StaticClassA = ConsoleApp1.ClassA;

class Program
{
    static void Main(string[] args)
    {
        string a = StaticClassA.A;
    }
}

Not too much to gain though, but it might ease your naming a little.虽然不会获得太多收益,但它可能会稍微简化您的命名。

Another (somewhat cooler) option is a static using :另一个(有点酷)选项是static using

using static ConsoleApp1.StaticClassA;

class Program
{
    static void Main(string[] args)
    {
        string a = A;
    }
}

You're attempting to do an anti-pattern there.你试图在那里做一个反模式。

Static properties are properties not defined in an instance (object) of that class, but by the class itself.静态属性不是在该类的实例(对象)中定义的属性,而是由该类本身定义的。 And as such, you can access and modify them whenever you choose to, provided you have the required scopes to do so.因此,您可以随时选择访问和修改它们,前提是您具有这样做所需的范围。

I don't see the problem in calling MyClass.StaticProperty = <some expression> , if indeed the functions the static property do, are static.我没有看到调用MyClass.StaticProperty = <some expression> ,如果静态属性确实执行的功能是静态的。 If it's something part of the object, something you don't connect with the class itself, ie it might be different for each instanced object of that class, then just turn it into a regular property instead.如果它是对象的一部分,您不与类本身连接的东西,即该类的每个实例化对象可能不同,那么只需将其转换为常规属性即可。

Example of some static properties and methods:一些静态属性和方法的示例:

public class DoMath
{
   public static string Pi { get; private set; } = "3.14";
   public static double X {get; set;}
   public static double Y {get; set;}

   public static double Sum() => X + Y;
}

DoMath.X = 3.5;
DoMath.Y = 4;
double result = DoMath.Sum();
Console.WriteLine($"Pi is equal to {DoMath.Pi}.");

If you truly wish something to be static, then don't try to make it non-static.如果你真的希望某些东西是静态的,那么不要试图让它变成非静态的。 Simply declare it as such.简单地声明它。

Static members are shared across all instances of the class or all instances of Class Of T of same T.静态成员在该类的所有实例或同一 T 的 Class Of T 的所有实例之间共享。

So you can access static properties outside of class by using the ClassName.VarName or directly by the VarName from within the class.因此,您可以使用ClassName.VarName或直接通过类内的VarName访问类外的静态属性。

You can access static fields and properties and methods from all non static methods.您可以从所有非静态方法访问静态字段、属性和方法。

You can also add an instance member mapping a static member.您还可以添加映射静态成员的实例成员。

Instances of a static thing can't exist in addition to the static existence itself.除了静态存在本身之外,静态事物的实例不能存在。

So you can write this:所以你可以这样写:

class myClass
{
  public string a = "hello";

  static public string b = "world";

  public string B { get { return b; } set { b = value; } }

  public void DoSomething()
  {
    b = "new world";
  }
}

And use it like that:并像这样使用它:

myClass instance= new myClass();
instance.DoSomething();
myClass.b = "another world";
instance.B = "C# world";

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

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