Do variables in static methods become static autom

2019-02-21 19:00发布

public static void DoSomething()
{
int a;
string b;

//..do something
}

In the example above, i have declared two variables. Do they become static because the method that contains them is static?

7条回答
神经病院院长
2楼-- · 2019-02-21 19:24

You can't have local static variables.

C# does not support static local variables (variables that are declared in method scope).

查看更多
贼婆χ
3楼-- · 2019-02-21 19:26

I am positive with your opinion but in the sample code below i'am taking an access violation exception about using protected memory. Because of that maybe it isn't support static local variables but in memory management it can point same address.

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 ;
        }
查看更多
手持菜刀,她持情操
4楼-- · 2019-02-21 19:34

No. Only the method is static but not variables.

From MSDN:

C# does not support static local variables (variables that are declared in method scope).

if you want to have static variable in static member, do the declaration outside the static method,

private static int _var = 0;
public static void SampleMethod()
{
     _var++;
} 
查看更多
聊天终结者
5楼-- · 2019-02-21 19:35

From MSDN

C# does not support static local variables (variables that are declared in method scope).

查看更多
做自己的国王
6楼-- · 2019-02-21 19:36

You can have "static" session-based variables within ASP.NET using System.Web.HttpContext.Current.Session.

Example:

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

}
查看更多
Explosion°爆炸
7楼-- · 2019-02-21 19:46

No, only the method is static.

From MSDN:

C# does not support static local variables (variables that are declared in method scope).

And here:

The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.

As you can see, local variables are not mentioned.

You can, however use a static field:

public class MyClass
{
    private static int MyVariable = 10;

    public static void MyMethod()
    {
      MyVariable++;
    }
}

A class can be static, and it can have static members, both functions and fields but not the variables in the static scope.

查看更多
登录 后发表回答