Tag: 静态

如何使用静态返回类型创建扩展方法?

我试图为Color静态类编写一个简单的扩展方法,它返回相当于该颜色的黑色和白色。 问题是扩展方法不能返回静态类型… 那么,我该怎么做?! 请帮我。

C#inheritance:静态与非静态字段

我有一个库基类( Controller )和三个inheritance自Controller ( Sensor , Output和Environment )的子类,其中: public class Controller { private SerialPort serial; private Sensor sensorSettings; private Output outputSettings; private Environment environmentSettings; protected Dictionary ErrorDescriptions { get{ this.errorDescriptions; } } protected SerialPort ControllerSerialPort { get{ this.serial; } } protected Sensor SensorSettings { get{ this.sensorSettings; } } // The other sub-class get properties. protected […]

使用静态构造函数在C#中创建单例

我一直在看c#中创建静态构造函数的文章: http://csharpindepth.com/Articles/General/Singleton.aspx 现在,他没有提到的一个选项是使用一个简单的静态构造函数。 做以下事情有什么问题吗? 如果它工作,它似乎比他复杂的解决方案恕我直言。 public static class ServiceFactory { static ServiceFactory() { container = new Foo(); } static Foo container; public static Instance { get { return container; } } } 我问的原因是我正在进行代码审查,并希望保持原样,如果没问题的话。

通过其粗俗的名称获取属性值

请考虑这个课程: public static class Age { public static readonly string F1 = “18-25”; public static readonly string F2 = “26-35”; public static readonly string F3 = “36-45”; public static readonly string F4 = “46-55”; } 我想编写一个获得“F1”并返回“18-25”的函数。例如 private string GetValue(string PropertyName) …. 我该怎么做?

C#访问静态函数中的非静态成员

所以我有一个function: List names = new string(); private static void getName(string name) { names.add(name); } 当我尝试编译时,我得到一个:’非静态字段’通知需要’对象引用。 我该怎么做才能使这个成员(名称)与getName兼容? 我需要它是非静态的或转换的,因为我想将结果放入其他非静态函数和表单中。

静态方法中的变量是否会自动变为静态,因为它们位于c#中的静态范围内?

public static void DoSomething() { int a; string b; //..do something } 在上面的例子中,我声明了两个变量。 它们是否变为静态,因为包含它们的方法是静态的?

c#中的静态类

在回答这个问题时( https://stackoverflow.com/questions/352317/c-coding-question#352327 ),它让我想知道…… 将静态类视为等效于实现单例模式的非静态类实例是否存在任何危险?

静态方法内存消耗

我有以下类,具有以下方法: public class Foo { public string A {get;set;} public static Foo New(string a) { Foo newFoo = new Foo(); newFoo.A = a; return newFoo; } } public class Bar { public void SomeMethod() { … Foo anotherFoo = Foo.New(“a”); …. } } 如果Bar类在使用上面的代码的过程中创建Foo,Foo是否会超出范围并获得垃圾收集,或者Foo(因为它使用静态方法)继续引用变量newFoo,因此anotherFoo永远不会去超出范围?

C#中的静态类,有什么优缺点?

我正在为我的学校作业编写一个小游戏,游戏是一个简单的2D游戏,有怪物,物品和子弹。 基本上你跑来跑去并试图收集所有物品硬币,怪物试图阻止你,你可以用你收集的子弹击落它们。 非常简单。 问题是,我已经将怪物,物品,墙壁,玩家和子弹添加到名为LiveObjects的静态类中,然后我可以从代码中的任何位置访问这些对象。 这是一种不好的做法吗? 什么是替代品? (它不是multithreading的) LiveObjects.cs internal static class LiveObjects { public static List items = new List(); // List with all the items public static List monsters = new List(); // List with all present monsters public static List walls = new List(); // List with the walls public static List bullets […]

如何获得WPF窗口的静态引用?

我已经尝试了很多方法来获取我的程序中窗口的静态引用。 我需要在运行时从不同的类访问其所有成员,因此需要静态引用。 我想要的是Program.Window1 ,其中Core是静态的, MyWindow是其静态成员之一。 在WinForms中,我通常在Program.cs中声明我的静态表单,但这似乎不适用于WPF及其自定义的“App.xaml”ApplicationDefinition。 我该怎么做? 注意:我已经尝试了很多方法:使用直接调用新窗口(即Program.Window1 = new Window1() )将不起作用,因为我得到一些线程无效exception。 据我所知,到目前为止,只有ApplicationDefinitions可以在WPF中启动窗口。 每当我尝试按“代码”而不是默认的XAML ApplicationDefinition的StartupUri创建一个窗口时,这是个例外: 调用线程必须是STA,因为许多UI组件都需要这个。