Tag: static methods

静态方法里面的HttpContext.Current.Response

我在静态类中有以下静态方法。 我的问题是在静态方法中使用HttpContext.Current.Response是否安全? 我想100%确定它是线程安全的,并且只与调用线程相关联。有人知道答案吗? public static void SetCookie(string cookieName, string cookieVal, System.TimeSpan ts) { try { HttpCookie cookie = new HttpCookie(CookiePrefix + cookieName) {Value = cookieVal, Expires = DateTime.Now.Add(ts)}; HttpContext.Current.Response.Cookies.Add(cookie); } catch (Exception) { return; } }

公共静态字符串MyFunc()上的“预期的类,委托,枚举,接口或结构”错误。 什么是“字符串”的替代品?

我尝试使用以下静态函数时收到错误。 错误: 预期的类,委托,枚举,接口或结构 function(和类): namespace MyNamespace { public class MyClass { // Some other static methods that use Classes, delegates, enums, interfaces, or structs public static string MyFunc(string myVar){ string myText = myVar; //Do some stuff with myText and myVar return myText; } } } 这导致编译器愤怒地(红色)为public static string的字符串部分加下划线。 所以,我认为这意味着string不是类,委托,枚举,接口或结构。 我可以使用什么而不是string来返回字符串或类似字符串的对象? 在C#中似乎没有String (大写字母S)类。 编辑 :括号与某些注释代码不匹配 – […]

为什么我只能从静态函数访问静态成员?

我在一个类中有一个静态函数。 每当我尝试使用非静态数据成员时,我都会遇到编译错误。 非静态字段,方法或属性成员需要对象引用 为什么它表现得那样?

C#接口不能包含运算符

任何人都可以解释为什么不允许C#接口包含运算符? 谢谢。

使用out参数反映静态重载方法

我有一些问题,通过reflection调用带有out参数的重载静态方法,并会欣赏一些指针。 我想要动态创建类似System.Int32或System.Decimal的类型,然后在其上调用静态TryParse(string, out x)方法。 以下代码有两个问题: t.GetMethod(“TryParse”, new Type[] { typeof(string), t } )无法返回我期望的MethodInfo mi.Invoke(null, new object[] { value.ToString(), concreteInstance })似乎成功但没有将out param concreteInstance设置为已解析的值 交织到此函数中,您可以看到一些临时代码,演示如果type参数设置为System.Decimal会发生什么。 public static object Cast(object value, string type) { Type t = Type.GetType(type); if (t != null) { object concreteInstance = Activator.CreateInstance(t); decimal tempInstance = 0; List l = new List(t.GetMethods(BindingFlags.Static | BindingFlags.Public)); […]

如何在Main()方法之前在C#中运行静态初始化方法?

给定一个带有初始化方法的静态类: public static class Foo { // Class members… internal static init() { // Do some initialization… } } 如何确保在Main()之前运行初始化程序? 我能想到的最好的是将它添加到Foo : private class Initializer { private static bool isDone = false; public Initializer() { if (!isDone) { init(); isDone = true; } } } private static readonly Initializer initializer = new Initializer(); 这有用还是有一些不可预见的警告? 还有更好的方法吗?

使用静态方法和变量 – 好与坏

我正在开发C#和asp.net Web应用程序。 我有一个名为utilities的通用类,我在这个公共实用程序类中有很多公共和静态变量。 由于这个数字逐渐增加,我想知道将实用程序方法和变量存储为公共静态是一种好的做法。 我的代码示例 public class utilities { public static string utilVariable1 = “Myvalue”; public static string utilVariable2 = “Myvalue”; public static string utilVariable3 = “Myvalue”; : public static string utilVariableN = “Myvalue”; public static string UtilMethod1() { //do something } public static string UtilMethod2() { //do something } public static string UtilMethodN() { […]

为什么静态方法有时为单独调用返回相同的结果?

在我的c#代码中,我有一个静态方法。 这是代码示例: public class RandomKey { public static string GetKey() { Random rng = new Random(); char[] chars = new char[8]; for (int i = 0; i < chars.Length; i++) { int v = rng.Next(10 + 26 + 26); char c; if (v < 10) { c = (char)('0' + v); } else if (v […]

可以在C#中覆盖静态方法吗?

我被告知static方法是隐式final ,因此不能被覆盖。 真的吗? 有人可以提供一个更好的覆盖静态方法的例子吗? 如果静态方法只是类方法,那么拥有它们的真正用途是什么?

C#静态变量 – 范围和持久性

我做了一个小实验: public abstract class MyClass { private static int myInt = 0; public static int Foo() { return myInt; } public static int Foo(int n) { myInt = n; return bar(); } private static int bar() { return myInt; } } 然后我跑了: MessageBox.Show(MyClass.Foo().ToString()); MessageBox.Show(MyClass.Foo(3).ToString()); MessageBox.Show(MyClass.Foo().ToString()); MessageBox.Show(MyClass.Foo(10).ToString()); MessageBox.Show(MyClass.Foo().ToString()); 我预期的结果是0,3,0,10,0。 令我惊讶的是,我得到了0,3,3,10,10。 这些变化持续多久? 程序执行的持续时间? 调用静态方法的函数的持续时间?