【问题标题】:Do variables in static methods become static automatically because they are within static scopes in c#?静态方法中的变量是否会自动变为静态,因为它们在 c# 中的静态范围内?
【发布时间】:2013-02-19 20:51:03
【问题描述】:
public static void DoSomething()
{
int a;
string b;

//..do something
}

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

【问题讨论】:

标签: c# variables methods static


【解决方案1】:

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

来自 MSDN:

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

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

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

【讨论】:

    【解决方案2】:

    尽管在 C 中可用,但在 C# 中不支持静态局部变量。

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

    【讨论】:

      【解决方案3】:

      来自MSDN

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

      【讨论】:

        【解决方案4】:

        我对您的意见持肯定态度,但在下面的示例代码中,我正在处理有关使用受保护内存的访问冲突异常。因此,它可能不支持静态局部变量,但在内存管理中它可以指向相同的地址。

        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 ;
                }
        

        【讨论】:

          【解决方案5】:

          你不能有局部静态变量。

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

          【讨论】:

            【解决方案6】:

            不,只有方法是静态的。

            来自MSDN

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

            还有here

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

            如您所见,没有提到局部变量。

            不过,您可以使用静态字段:

            public class MyClass
            {
                private static int MyVariable = 10;
            
                public static void MyMethod()
                {
                  MyVariable++;
                }
            }
            

            一个类可以是静态的,它可以有静态成员,包括函数和字段,但不能是静态范围内的变量。

            【讨论】:

              【解决方案7】:

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

              例子:

              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
              
              }
              

              【讨论】:

                猜你喜欢
                • 2011-05-30
                • 1970-01-01
                • 1970-01-01
                • 2014-03-23
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-06-24
                相关资源
                最近更新 更多