c#静态类和非静态有什么区别(我说的是类本身而不是字段)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1276333/
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-06 14:33:25  来源:igfitidea点击:

c# What is the different between static class and non-static (I am talking about the class itself not the field)

c#classstatic

提问by Athiwat Chunlakhan

The syntax maybe wrong

语法可能不对

public static class Storage
{
    public static string filePath { get; set; }
}

And

public class Storage
{
    private void Storage () {};
    public static string filePath { get; set; }
}

I got this from an example on the internet. what is the use of the second one?

我从互联网上的一个例子中得到了这个。第二个有什么用?

采纳答案by Fredrik M?rk

If you look at the IL code, the static class will be abstractand sealedwhich gives two important qualities:

如果您查看 IL 代码,静态类将是abstract并且sealed它提供了两个重要的品质:

  • You cannot create instances from it
  • It cannot be inherited
  • 你不能从它创建实例
  • 它不能被继承

A consequence of the first point is that a static class cannot contain non-static members. There may be many uses of static members in a non-static class. One common use is to have a class factory:

第一点的结果是静态类不能包含非静态成员。在非静态类中可能有很多静态成员的用途。一个常见的用途是拥有一个类工厂:

public class SomeClass
{
    public int SomeInt { get; set; }

    public static SomeClass Create(int defaultValue)
    {
        SomeClass result = new SomeClass();
        result.SomeInt = defaultValue;
        return result;
    }
}

回答by Spencer Ruport

Lots of classes have both instance and static methods. String for example has:

许多类都有实例方法和静态方法。字符串例如有:

String.Format(string, arg0, arg1, arg2) // static method

And

String myString = "    Hello world!";
myString = myString.Substring(4);       // instance method

If you're asking why both the class and the method need the static keyword it's simply by design. I see what you're asking, if the class is static then of course all the methods are static as well, seems kind of redundant to put it there twice. I don't know if there's a good reason for that or not.

如果您问为什么类和方法都需要 static 关键字,那只是设计使然。我明白你在问什么,如果类是静态的,那么当然所有的方法也是静态的,把它放在那里两次似乎有点多余。我不知道这样做是否有充分的理由。

回答by German Latorre

When you declare a class as static:

当您将类声明为static 时

  • It is allowed to have only static members,
  • It cannot be instantiated(it has no public constructor), and
  • It cannot be inherited(it's sealed).
  • 允许有静态成员
  • 不能被实例化(它没有公共构造函数),并且
  • 不能被继承(它是密封的)。

Any class which is not declared as static can be instantiated, inherited, and can have non-static members.

任何未声明为静态的类都可以被实例化、继承,并且可以具有非静态成员。

回答by Gishu

Here is the official/MSDN hot-spot to learn about static classes

这里是学习静态类的官方/MSDN热点

The main features of a static class are:
* They only contain static members.
* They cannot be instantiated.
* They are sealed.
* They cannot contain Instance Constructors

静态类的主要特点是:
* 它们只包含静态成员。
* 它们不能被实例化。
* 他们是密封的。
* 它们不能包含实例构造函数

Basically a static class is identical to a 'normal'/non-static class which has only static methods and a private ctor. Marking it as static helps clarify intent and helps the compiler do some compile-time checks to disallow certain things e.g. disallow instantiation.

基本上,静态类与只有静态方法和私有构造函数的“普通”/非静态类相同。将其标记为静态有助于澄清意图并帮助编译器进行一些编译时检查以禁止某些事情,例如禁止实例化。

Real-world uses I can think of: Use it to house or as a way to organize

我能想到的实际用途:用它来安置或作为一种组织方式

  • utility methods (methods not associated with any instance of a type) e.g. Math for Min and Max methods
  • extension methods e.g. StopWatchExtensions for a Reset method on a StopWatch
  • 实用方法(与类型的任何实例无关的方法)例如 Min 和 Max 方法的 Math
  • 扩展方法,例如 StopWatchExtensions 用于 StopWatch 上的 Reset 方法

回答by Charlie

Static classes are only available from C#2 upwards. In C#1 you would have to seal your class and specify that it is not instantiable by added a private constructor to get this kind of behaviour.

静态类仅从 C#2 起可用。在 C#1 中,您必须密封您的类并通过添加私有构造函数来指定它不可实例化以获得此类行为。

回答by JavaResp

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be 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.

静态类与非静态类基本相同,但有一个区别:静态类不能被实例化。换句话说,您不能使用 new 关键字来创建类类型的变量。由于没有实例变量,您可以使用类名本身访问静态类的成员。

public static class Storage
{
   public static string filePath { get; set; }
}

in this,the class need not to be instantiate.so same with the filepath ,it will occupy unique value of class Storage for all object.

在这种情况下,类不需要实例化。因此与文件路径相同,它将为所有对象占用类 Storage 的唯一值。

public class Storage
{
    private void Storage {};
    public static string filePath { get; set; }
}  

in this,the class is non static,need to be instantiate

在这种情况下,该类是非静态的,需要实例化

回答by Somya Chaurasia

As we know variables and functions are of two types-instance and class.

我们知道变量和函数有两种类型——实例和类。

A static class -has only class variables no instance variables.

静态类 - 只有类变量没有实例变量。

Hence cannot be instantiated,only accessible by Classname.method().

因此无法实例化,只能通过 Classname.method() 访问。

It contains only Private constructors no public constructor is there.

它只包含私有构造函数,没有公共构造函数。

A static class contains only static members.

静态类只包含静态成员。