Tag: static

决定使C#类静态

如果我在C#中有一个Utilities.cs类,它只包含静态方法 例如 public static string DoSomething() { return “something”; } 我应该让这个类本身是静态的吗? public static Utilities() { … 使类静态有什么优势吗?

哪个有更好的表现? 静态与对象

我设计了一个C#控制台应用程序,使用OOP设计合并和拆分大文件(大小约4GB)。 它涉及读/写xml,平面文件和图像。 我为读者和作家提供课程。 合并时间约为00:12,而分裂时间超过04:30。 然后,我通过将输出文件分发到子目录而不是使用单个目录,将分割的性能提高到00:50。 我的老板要求我将所有内容转换为静态过程编程,而不是对象。 他说00:12合并比较00:50分裂是不平衡的。 他希望在00:30分钟完成分裂,转换成静态。 现在我知道静态调用会更快。 但是我不同意所有静态都会更好,因为我必须在方法中使用“ref”和“out”参数。 我的问题是: 将文件拆分到子目录的原因比使用单个输出目录快得多? (即大量文件> 200,000) 有没有比将代码从对象转换为静态更好的方法,以实现更高的性能?

将CommandBindings添加到控件与使用RegisterClassCommandBinding之间有区别吗?

以前我一直在使用 this.CommandBindings.Add( new CommandBinding(ApplicationCommands.Copy, this.cmdCopy_Executed, this.cmdCopy_CanExecute)) 其中cmdCopy_Executed是一个非静态函数,但我见过人们使用 static MyControl() { CommandBinding binding = new CommandBinding(ApplicationCommands.Save, CommandHandler); CommandManager.RegisterClassCommandBinding(typeof(MyControl), binding); } private static void CommandHandler(object target, ExecutedRoutedEventArgs e) { MessageBox.Show(“Command Handled!”); } CommandBinding是静态的。 一个优先于另一个?

静态类成员是否固定?

我有一个C#类,有一个静态的ImageList对象。 此图像列表将与我的应用程序中的多个表单上的各种ListView标头(通过SendMessage … HDM_SETIMAGELIST)共享。 虽然我知道静态对象不符合垃圾收集的条件,但我不清楚它们是否也不符合垃圾收集器的重定位(压缩)条件。 我是否还需要固定此对象,因为它与非托管代码共享,例如,使用GCHandle.Alloc? 环境是VS 2008,Compact Framework 3.5。

通过接口使用静态类?

想象一下,您需要从整个应用程序中访问一些方法。 静态类是理想的。 public static class MyStaticClass { public static void MyMethod() { // Do Something here… } } 但也许将来我会在另一个静态类中添加静态方法的第二个实现。 public static class MyStaticClass2 { public static void MyMethod() { // Do Something here… } } 有没有办法改变我的其他代码中使用的静态类而不更改MyStaticClass.MeMethod();的调用MyStaticClass.MeMethod(); 到MyStaticClass2.MyMethod(); ? 我想到了一个界面,但我不知道如何实现这个……如果我说疯了就说出来,我只会改变电话:D

我应该为每个Web请求使用静态缓存的ResourceManager还是新实例? 有关系吗?

在使用new ResourceManger(myResourceType.FullName, myResourceType.Assembly)和使用new ResourceManger(myResourceType.FullName, myResourceType.Assembly)生成的类中的“缓存的ResourceManager实例”的每个请求上创建新的.NET ResourceManager的性能(或其他)含义(如果有的new ResourceManger(myResourceType.FullName, myResourceType.Assembly) ( MyResourceType.ResourceManager )? 我正在使用.resx文件在ASP.NET MVC 3应用程序的上下文中工作。 编辑:我感兴趣的是除了为新对象分配内存的成本之外的影响。 编辑:查看ResourceManager.ReleaseAllResources的MSDN文档,它指出: This method will shrink the working set in a running application. Any future resource lookups on this ResourceManager will be as extensive as the first lookup, since it will need to search and load resources again. 这似乎意味着资源集的初始开放是昂贵的,这表明在每个请求上创建一个新的经理可能是昂贵的。 但是,文档并未提出有关资源管理器生命周期/范围的最佳实践。

适当使用静态方法

从概念上讲,当方法只接受输入并将输入重新格式化为输出时,使用静态方法(C#)是否合适? 例如: public static string FormatString(string inputString){ return “some formatting” + inputString + “Some other formatting”; } 如果我有一些这样的方法,静态的“实用”类是一个好主意吗?

用静态字段切换语句

假设我有一堆静态字段,我想在交换机中使用它们: public static string PID_1 = “12”; public static string PID_2 = “13”; public static string PID_3 = “14”; switch(pid) { case PID_1: //Do something 1 break; case PID_2: //Do something 2 break; case PID_3: //Do something 3 break; default: //Do something default break; } 由于C#不允许在开关内部使用非const语句。 我想了解这种设计的意图是什么。 我应该如何在c#中执行上述操作?

在静态类中访问HttpContext.Current

我可以在静态类和Method中调用HttpContext.Current吗? 我想在每个用户的基础上存储一个值,但希望能够以静态方式访问它。 这会有用吗? public static class StaticClass { public static string SomeThing { get { return HttpContext.Current.Items[“SomeItem”].ToString(); } } }

私有嵌套静态类 – 好或坏的做法?

将私有静态类嵌套在非静态类中会被认为是一种不好的做法吗? public class Outer { private static class Inner { } } 这里的想法是’Outer’的所有实例都将共享对静态的访问。 另一种方法可能是让Inner类是非静态的并使用它的静态实例: public class Outer { private static innerInstance = new Inner(); private class Inner { } } 类似的效果。 这种方法有哪些优缺点或其他考虑因素? 我必须承认,我几乎从不使用嵌套类,无论是否静态,但我对这个特定的概念感兴趣。