c#单独分类存储
我使用多个类,我需要一个。。。假设所有类和方法的全局存储。 为存储创建静态类是否正确c#单独分类存储,c#,class,storage,C#,Class,Storage,我使用多个类,我需要一个。。。假设所有类和方法的全局存储。 为存储创建静态类是否正确 public static class Storage { public static string filePath { get; set; } } 或者还有其他方法吗?您应该看看存储库模式: 实现此模式的一种方法是使用ORM的s.a.NHibernate: 您应该看看存储库模式: 实现此模式的一种方法是使用ORM的s.a.NHibernate: 可以考虑使用单体设计
public static class Storage
{
public static string filePath { get; set; }
}
或者还有其他方法吗?您应该看看存储库模式: 实现此模式的一种方法是使用ORM的s.a.NHibernate:
您应该看看存储库模式: 实现此模式的一种方法是使用ORM的s.a.NHibernate:
可以考虑使用单体设计模式:
例如你可以考虑使用单体设计模式:
例如 如果你真的需要让你的例子成为一个单身汉,那么这里是你如何做到的public class StorageSingleton
{
private static readonly StorageSingleton instance;
static StorageSingleton() {
instance = new Singleton();
}
// Mark constructor as private as no one can create it but itself.
private StorageSingleton()
{
// For constructing
}
// The only way to access the created instance.
public static StorageSingleton Instance
{
get
{
return instance;
}
}
// Note that this will be null when the instance if not set to
// something in the constructor.
public string FilePath { get; set; }
}
调用和设置singleton的方法如下:
// Is this is the first time you call "Instance" then it will create itself
var storage = StorageSingleton.Instance;
if (storage.FilePath == null)
{
storage.FilePath = "myfile.txt";
}
或者,您可以向构造函数中添加以下内容以避免空引用异常:
// Mark constructor as private as no one can create it but itself.
private StorageSingleton()
{
FilePath = string.Empty;
}
警告语;从长远来看,使任何东西全球化或单一化都会破坏您的代码。稍后,您真的应该检查存储库模式。如果您真的需要将您的示例变成一个单例,那么下面是您如何做到这一点的
public class StorageSingleton
{
private static readonly StorageSingleton instance;
static StorageSingleton() {
instance = new Singleton();
}
// Mark constructor as private as no one can create it but itself.
private StorageSingleton()
{
// For constructing
}
// The only way to access the created instance.
public static StorageSingleton Instance
{
get
{
return instance;
}
}
// Note that this will be null when the instance if not set to
// something in the constructor.
public string FilePath { get; set; }
}
调用和设置singleton的方法如下:
// Is this is the first time you call "Instance" then it will create itself
var storage = StorageSingleton.Instance;
if (storage.FilePath == null)
{
storage.FilePath = "myfile.txt";
}
或者,您可以向构造函数中添加以下内容以避免空引用异常:
// Mark constructor as private as no one can create it but itself.
private StorageSingleton()
{
FilePath = string.Empty;
}
警告语;从长远来看,使任何东西全球化或单一化都会破坏您的代码。稍后,您真的应该检查存储库模式。将Singleton应用于原始类:
public class Storage
{
private static Storage instance;
private Storage() {}
public static Storage Instance
{
get
{
if (instance == null)
{
instance = new Storage();
}
return instance;
}
}
public string FilePath { get; set; }
}
用法:
string filePath = Storage.Instance.FilePath;
将Singleton应用于原始类:
public class Storage
{
private static Storage instance;
private Storage() {}
public static Storage Instance
{
get
{
if (instance == null)
{
instance = new Storage();
}
return instance;
}
}
public string FilePath { get; set; }
}
用法:
string filePath = Storage.Instance.FilePath;
我喜欢在C#中看到singleton的实现 C#保证实例不会被重写,静态构造函数保证在第一次使用静态属性之前将其实例化
好处:按照静态构造函数的语言设计,它是线程安全的,没有双重检查锁定:)。我喜欢在C#中看到singleton的实现 C#保证实例不会被重写,静态构造函数保证在第一次使用静态属性之前将其实例化
奖励:根据静态构造函数的语言设计,它是线程安全的,没有双重检查锁定:)。谢谢这很快,我现在正在研究它。但对于你的例子,我认为这对我来说太多了。我根本不需要那种存储。谢谢,那太快了,我现在正在研究它。但是对于你的例子,我觉得它对我来说太多了。我根本不需要那种存储。你的意思是说你想要一个全局变量,通过它你可以访问对象和方法吗?如果是这样的话,您可能会研究使用单例。您的意思是说您想要一个全局变量,通过它您可以访问对象和方法吗?如果是这样,您可能会研究使用单例。