传递静态参数的一类 [英] Passing static parameters to a class
本文介绍了传递静态参数的一类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
据我知道你能不能传递参数,在C#静态构造函数。
但是我确实有2个参数我需要传递,并将其分配到静态字段之前,我创建一个类的实例。我怎么做呢?
As far as I know you can can't pass parameters to a static constructor in C#. However I do have 2 parameters I need to pass and assign them to static fields before I create an instance of a class. How do I go about it?
推荐答案
这可能是一个......工厂方法的调用!
This may be a call for ... a Factory Method!
class Foo
{
private int bar;
private static Foo _foo;
private Foo() {}
static Foo Create(int initialBar)
{
_foo = new Foo();
_foo.bar = initialBar;
return _foo;
}
private int quux;
public void Fn1() {}
}
您可能希望把一检查'吧'已初始化(或没有)为宜。
You may want to put a check that 'bar' is already initialized (or not) as appropriate.
这篇关于传递静态参数的一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文