C# 什么是“静态”类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/695056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:47:33  来源:igfitidea点击:

What is a "static" class?

c#static-classes

提问by Jeremy H

In C# what is the difference between:

在 C# 中,有什么区别:

public static class ClassName {}

And:

和:

public class ClassName {}

采纳答案by lmsasu

A static class cannot be instantiated, and can contain only static members. Hence, the calls for a static class are as: MyStaticClass.MyMethod(...)or MyStaticClass.MyConstant.

静态类不能被实例化,只能包含静态成员。因此,对静态类的调用如下:MyStaticClass.MyMethod(...)MyStaticClass.MyConstant

A non static class can be instantiated and may contain non-static members (instance constructors, destructor, indexers). A non-static member of a non-static class is callable only through an object:

非静态类可以被实例化并且可能包含非静态成员(实例构造函数、析构函数、索引器)。非静态类的非静态成员只能通过对象调用:

MyNonStaticClass x = new MyNonStaticClass(...);
x.MyNonStaticMethod(...);

回答by Konstantin Tarkus

Static class can contain static members only.

静态类只能包含静态成员。

Static member can be used without instantiating a class first.

无需先实例化类即可使用静态成员。

回答by Mark Simpson

All methods/properties in a static class mustbe static, whereas a 'normal' class can contain a mix of instance and static methods.

静态类中的所有方法/属性都必须是静态的,而“普通”类可以包含实例方法和静态方法的混合。

回答by Henk Holterman

You can't instantiate (create objects of) a static class. And it can only contain static members.

您不能实例化(创建对象)静态类。并且它只能包含静态成员。

Example: System.Math

示例:System.Math

回答by Dan Herbert

A static class also can not be inherited from, whereas a non-static class with static members can be inherited from.

静态类也不能被继承,而具有静态成员的非静态类可以被继承。

回答by CMS

Static classes and members are used to create data and methods that can be accessed without creating an instance (using the newkeyword, they cannot have a constructor) of the class.

静态类和成员用于创建无需创建类的实例(使用new关键字,它们不能有构造函数)即可访问的数据和方法。

Static classes can be declared when there is no dependence on the its own object identity, so a static class must contain only static members.

静态类可以在不依赖自身对象标识的情况下声明,因此静态类必须只包含静态成员。

This classes are loaded by the CLR when the program or namespace containing the class is loaded.

当加载包含类的程序或命名空间时,这些类由 CLR 加载。

They are also sealed, cannot be inherited from.

它们也是密封的,不能继承。

回答by Jon Skeet

Firstly, a comment on an answer asked about what "static" means. In C# terms, "static" means "relating to the type itself, rather than an instance of the type." You access a static member (from another type) using the type name instead of a reference or a value. For example:

首先,对回答的评论询问“静态”是什么意思。在 C# 术语中,“静态”意味着“与类型本身有关,而不是与类型的实例相关”。使用类型名称而不是引用或值访问静态成员(来自另一种类型)。例如:

// Static method, so called using type name
Guid someGuid = Guid.NewGuid();
// Instance method, called on a value
string asString = someGuid.ToString();

Now, static classes...

现在,静态类...

Static classes are usually used as "utility" classes. The canonical example is probably System.Math. It doesn't make sense to create an instance of math - it just "is". A few rules (both "can" and "can't"):

静态类通常用作“实用程序”类。典型的例子可能是System.Math. 创建一个数学实例是没有意义的——它只是“是”。一些规则(“可以”和“不能”):

  • Static classes always derive from object. You can't specify a different base type, or make the static class implement an interface.
  • Static classes can't have any instance members - all variables, methods etc must be static.
  • Static classes can't declare any instance constructors and the compiler doesn'tcreate a parameterless constructor by default. (Before static classes came in C# 2.0, people would often create an abstract class with a private constructor, which prevented instantiation. No need here.)
  • Static classes are implicitly abstract (i.e. they're compiled to IL which describes an abstract class) but you can't add the abstractmodifier yourself.
  • Static classes are implicitly sealed (i.e. they're compiled to IL which describes an sealed class) but you can't add the sealedmodifier yourself.
  • Static classes maybe generic.
  • Static classes maybe nested, in either non-static or static classes.
  • Static classes mayhave nested types, either non-static or static.
  • Only static, top-level non-generic classes can contain extension methods (C# 3.0).
  • 静态类总是从object. 您不能指定不同的基类型,或使静态类实现接口。
  • 静态类不能有任何实例成员——所有变量、方法等都必须是静态的。
  • 静态类不能声明任何实例构造函数,默认情况下编译器不会创建无参数构造函数。(在 C# 2.0 中出现静态类之前,人们通常会创建一个带有私有构造函数的抽象类,这会阻止实例化。这里不需要。)
  • 静态类是隐式抽象的(即它们被编译为描述抽象类的 IL),但您abstract不能自己添加修饰符。
  • 静态类是隐式密封的(即它们被编译为描述密封类的 IL),但您sealed不能自己添加修饰符。
  • 静态类可能是通用的。
  • 静态类可以嵌套在非静态或静态类中。
  • 静态类可能有嵌套类型,非静态或静态。
  • 只有静态的顶级非泛型类可以包含扩展方法 (C# 3.0)。

回答by Syed Tayyab Ali

public static class ClassName {}

A static class is just like a global variable: you can use it anywhere in your code without instantiating them. For example: ClassName. After the dot operator, you can use any property or function of it.

静态类就像一个全局变量:您可以在代码中的任何地方使用它而无需实例化它们。例如:类名。在点运算符之后,您可以使用它的任何属性或函数。

 public class ClassName {}

But if you have non-static class then you need to create an instance of this class. For example:

但是如果你有非静态类,那么你需要创建这个类的一个实例。例如:

 ClassName classNameObject = new ClassName(); 

回答by Sanjeev Kumar Dangi

http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html- very good article on this. This is for Java. But i think concept should should same in C# too.

http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html- 非常好的文章。这是针对 Java 的。但我认为 C# 中的概念也应该相同。

回答by richu

Static variable in c

c中的静态变量

a variable local to a class as auto variables but static variable do not disappear as function is no longer active.Their values persist.If control comes back,static variables have same value

类的局部变量作为自动变量,但静态变量不会因为函数不再活动而消失。它们的值保持不变。如果控制回来,静态变量具有相同的值

static function in cfunctions that are not visible to functions in other files.

c函数中的静态函数对其他文件中的函数不可见。

*static data members in cpp *data members can be variables or functions in cpp static applies to both data members the class itself can be static "There is only one copy of static data memberss shared by all objects in that class" static data members can access only static data members

* cpp 中的静态数据成员 * cpp 中的数据成员可以是变量或函数 静态适用于两个数据成员 类本身可以是静态的“该类中的所有对象都只共享一个静态数据成员的副本”静态数据成员可以只访问静态数据成员

static classthis class cannot instantiate objects

静态类这个类不能实例化对象