简体   繁体   English

C#语法静态类,使静态成员冗余吗?

[英]C# Syntax static class, makes static members redundant?

Note: This is the first time asking a question about a general curiosity regarding syntax. 注意:这是第一次询问有关语法的一般好奇心的问题。 Because of this I might not be using the correct terminology to describe my question, which may mean its already been answered, and I can't find it because I'm unaware of what terms to use in the search. 因此,我可能没有使用正确的术语来描述我的问题,这可能意味着已经回答了该问题,由于我不知道在搜索中使用哪些术语,因此无法找到它。 If that is the case, please post comments so I can edit and refine the question to meet the standard expected by stack overflow. 如果是这种情况,请发表评论,以便我可以编辑和完善问题以符合堆栈溢出所期望的标准。 Thank you. 谢谢。

Now the question. 现在的问题。 I've been using the static keyword a lot recently because I have members I need to access from anywhere without an instance. 我最近一直在使用static关键字,因为我有需要从任何地方访问而没有实例的成员。 However its becoming increasingly tedious to declare every member and method static, when the class its self is already static. 但是,当类的自身已经是静态的时,声明每个成员和方法是静态的变得越来越繁琐。 Since if you declare a class static, it means you can't create an instance of that class (that's my current understanding). 由于如果您声明一个类为静态,则意味着您无法创建该类的实例(这是我目前的理解)。

public static class Foo {}

Why does every member have to also be declared static. 为什么每个成员也必须声明为静态。

public static class Foo 
{
    public static int X;
    public static int Y;
}

I would have thought that since the class, foo in this case, is declared to be static, all its members would automatically be static, and you no longer need to declare each member as static. 我会以为,既然在这种情况下将类foo声明为静态的,则其所有成员将自动为静态的,并且您不再需要将每个成员声明为静态的。

Obviously you can't do that, you have to declare every subsequent member static. 显然,您不能这样做,您必须将每个后续成员声明为静态。 However this feels counter intuitive and redundant to me. 但是,这对我来说感觉很直觉而且多余。

Whats the reason for this? 是什么原因呢?

Put simply -- it's for readability. 简而言之-是为了提高可读性。 You can look solely at the property definition and know that it's static, without having to look at the class definition. 您可以只查看属性定义,并知道它是静态的,而不必查看类定义。

Your two-line example may make it look unnecessary, but if you have a class with 500, 1000 or more lines of code, you will appreciate the extra clarity when digging through lots of methods, properties and fields. 您的两行示例可能看起来不必要,但是如果您的类包含500、1000或更多行代码,则在深入研究许多方法,属性和字段时会更加清楚。

That's the thing about software development: Never ever worry about a bit of extra typing -- you will read your code many more times than you will write it. 这就是软件开发的本质:永远不必担心额外的输入-您代码的次数比编写代码的次数多。

From what I understand, static on the class is a way to enforce all members are static (remove static from one, and it should give you an error). 据我了解,类上的static是一种强制所有成员都是st​​atic的方法(将static从一个成员中删除,它应该会给您一个错误)。 But yes, there is the requirement of defining the methods static. 但是,是的,需要定义静态方法。 Technically, you wouldn't need to define static on the class. 从技术上讲,您无需在类上定义静态。

You could, as an alternative, define a class, instantiate the class, and store the class reference globally, thus not needing the static definition on each member. 作为替代方案,您可以定义一个类,实例化该类并全局存储该类引用,因此不需要在每个成员上进行静态定义。 The singleton pattern acts very similar in nature to static classes, except you define one common instance shared throughout. 单例模式在本质上与静态类的行为非常相似,不同之处在于您定义了一个始终共享的公共实例。

For consistency - uniformity of meaning. 为了一致性-意义的一致性。 Making a class static doesn't change anything about the class; 将类设为静态不会改变该类的任何内容。 all it does is forbid the declaration of instance members. 它所做的一切都禁止声明实例成员。 The class itself is neither instance nor static. 该类本身既不是实例也不是静态的。 It would be more concise if static on a class implied all members static, but one could argue that doing so would introduce a variable interpretation for a member declaration where not declared static. 如果一个类上的static隐含所有成员都是st​​atic,那会更加简洁,但是有人会争辩说,这样做会为未声明static的成员声明引入变量解释。 As it is, members are instance unless the static modifier is used. 实际上,除非使用static修饰符,否则成员都是实例。

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

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