C#什么是静态类和非静态之间的不同(我说的是类本身不是字段) [英] c# What is the different between static class and non-static (I am talking about the class itself not the field)
本文介绍了C#什么是静态类和非静态之间的不同(我说的是类本身不是字段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
语法可能错了。
public static class Storage
{
public static string filePath { get; set; }
}
和
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?
推荐答案
如果你看看IL代码,静态类会为摘要
和密封
这给出了两个重要的素质:
If you look at the IL code, the static class will be abstract
and sealed
which gives two important qualities:
- 您不能从它
- 它不能被继承
第一点的结果是,一个静态类不能包含非静态成员。有可能在一个非静态类的静态成员的许多用途。一个常见的用途是有一个类工厂:
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;
}
}
这篇关于C#什么是静态类和非静态之间的不同(我说的是类本身不是字段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文