静态方法中的变量会自动变为静态,因为它们在c#中的静态作用域中


public static void DoSomething()
{
int a;
string b;
//..do something
}

在上面的例子中,我声明了两个变量。它们变成静态是因为包含它们的方法是静态的吗?

No。只有方法是静态的,而不是变量。

从MSDN:

c#不支持静态局部变量(在方法作用域中声明的变量)。

如果你想在静态成员中有静态变量,在静态方法之外进行声明,

private static int _var = 0;
public static void SampleMethod()
{
     _var++;
} 

虽然静态局部变量在C语言中可用,但在c#中不支持。

如果你想要一个等效的局部静态变量,你可以在类上创建一个实例变量,或者一个静态变量。否则,请考虑该方法本身是否属于静态类,以及它是否应该属于另一个类型。

From MSDN

c#不支持静态局部变量在方法范围内声明)。

我对你的意见是积极的,但在下面的示例代码中,我正在使用受保护内存的访问违反异常。正因为如此,它可能不支持静态局部变量,但在内存管理中,它可以指向相同的地址。

public static byte[] RawSerialize(object anything)
        {
                int rawsize = Marshal.SizeOf(anything);
                IntPtr buffer = Marshal.AllocHGlobal(rawsize);
                Marshal.StructureToPtr(anything, buffer, false);
                byte[] rawdata = new byte[rawsize];
                Marshal.Copy(buffer, rawdata, 0, rawsize);
                Marshal.FreeHGlobal(buffer);
                return rawdata ;
        }

不能使用局部静态变量

c#不支持静态局部变量(在方法作用域中声明的变量)。

不,只有方法是静态的。

从MSDN:

c#不支持静态局部变量

:

静态修饰符可用于类、字段、方法、属性、操作符、事件和构造函数,但不能使用使用索引器、析构函数或类以外的类型。

可以看到,这里没有提到局部变量。

但是,您可以使用静态字段:
public class MyClass
{
    private static int MyVariable = 10;
    public static void MyMethod()
    {
      MyVariable++;
    }
}

类可以是静态的,它可以有静态成员,包括函数和字段,但不能有静态作用域中的变量。

您可以在ASP中使用基于会话的"静态"变量。System.Web.HttpContext.Current.Session.

的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SomeNameSpace
{
    public static class CSession
    {
        private static readonly string zE = "";
        private static readonly string CrLF = Environment.NewLine;

        /// <summary>
        /// 
        /// </summary>
        public static bool HasSession { get { return HttpContext.Current != null && HttpContext.Current.Session != null; } }

        /// <summary>
        /// Get a session variable
        /// </summary>
        /// <param name="pSessionKey"></param>
        /// <returns></returns>
        public static object Get(string pSessionKey)
        {
            object t = null;
            try
            {
                if (HasSession && HttpContext.Current.Session[pSessionKey] != null) { t = (object)HttpContext.Current.Session[pSessionKey]; }
            }
            catch (Exception ex) { t = null; string m = ex.Message; }
            return t;
        }//object Get(string pSessionKey)

        /// <summary>
        /// Set a session variable
        /// </summary>
        /// <param name="pSessionKey"></param>
        /// <param name="pObject"></param>
        public static void Set(string pSessKey, object pObject)
        {
            if(!HasSession) { return; }
            HttpContext.Current.Session.Remove(pSessKey);
            HttpContext.Current.Session.Add(pSessKey, pObject);
        }//void Set(string pSessionKey, object pObject)

        public static string GetString(string pSessKey)
        {
            string sTemp = zE;
            object t = Get(pSessKey);
            if (t != null) { sTemp = (string)t; } else { sTemp = zE; }
            return sTemp;
        }//string GetString(string pSessionKey)

        public static int GetInt(string pSessKey)
        {
            int s = 0;
            object t = Get(pSessKey);
            if (t != null) { s = (int)t; }
            return s;
        }//int GetInt(string pSessionKey)

        public static Int32 GetInt32(string pSessKey)
        {
            Int32 s = 0;
            object t = Get(pSessKey);
            if (t != null) { s = (Int32)t; }
            return s;
        }//Int32 GetInt32(string pSessionKey)

        public static bool GetBool(string pSessKey)
        {
            bool s = false;
            object t = Get(pSessKey);
            if (t != null) { s = (bool)t; }
            return s;
        }//bool GetBool(string pSessionKey)
    }//public static class CSession
}

相关内容

最新更新