欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# 靜態(tài)類的使用場(chǎng)景

 更新時(shí)間:2021年01月04日 15:52:25   作者:Prozkb  
這篇文章主要介紹了c# 靜態(tài)類的使用場(chǎng)景,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下

判斷這個(gè)很簡(jiǎn)單,就是從內(nèi)存的優(yōu)化方面去考慮.因?yàn)殪o態(tài)和非靜態(tài)的不同的地方,就是靜態(tài)的從程序一啟動(dòng)就會(huì)一直占用內(nèi)存,而非靜態(tài)的只在使用后(實(shí)例化)后才會(huì)占用內(nèi)存.但是每實(shí)例化個(gè)一個(gè)對(duì)象時(shí)又會(huì)另外占用內(nèi)存. 舉個(gè)例子,比如說(shuō)一個(gè)數(shù)據(jù)庫(kù)的連接字段(STRING).因?yàn)橐?jīng)常使用到它,這時(shí)我們可以用STATIC.但是如果這時(shí)用非靜態(tài)的話那就不合算了,因?yàn)槊看握{(diào)用到它時(shí),又實(shí)例化一次.這樣相比來(lái)說(shuō)占用內(nèi)存就比較大了.不劃算.  像一個(gè)登錄后臺(tái)的方法,你只在登陸時(shí)候調(diào)用一次,就沒(méi)有必要做成靜態(tài)的了.那樣一直駐存在內(nèi)存中.在大型項(xiàng)目中,你如果都使用靜態(tài)的那得要多少內(nèi)存去支撐呀.嘿嘿  簡(jiǎn)單點(diǎn),就是它經(jīng)常要被調(diào)用時(shí),就用靜態(tài)的.相反則用非靜態(tài)的.

那也就是說(shuō),公共方法就設(shè)置為靜態(tài)的方法唄。果然是的,看經(jīng)常用的comm類就是靜態(tài)的Static

public class fnComm
  {    
    public static JObject Post(HttpClient myhttp, string url, JObject json)
    {
      HttpContent content = new StringContent(JsonConvert.SerializeObject(json), Encoding.UTF8, "application/json");
      var message = Task<HttpResponseMessage>.Run<HttpResponseMessage>(() =>
      {
        return myhttp.PostAsync(url, content);
      });
      message.Wait();
      //接收返回得信息
      if (message.Result.IsSuccessStatusCode)
      {
        var s = Task.Run(() =>
        {
          return message.Result.Content.ReadAsStringAsync();
        });
        s.Wait();
        return JObject.Parse(s.Result);
      }
      else
      {
        throw new Exception("StatusCode:" + message.Result.StatusCode.ToString());
      }
    }
    public static byte[] ConvertToByteAry(object obj)
    {
      var j = JsonConvert.SerializeObject(obj);
      var ary = System.Text.Encoding.UTF8.GetBytes(j);
      return ary;
    }

    /// <summary>
    /// datetime轉(zhuǎn)換為unixtime
    /// </summary>
    /// <param name="time"></param>
    /// <returns></returns>
    public static int TimeToUnixTime(System.DateTime time)
    {
      return (int)(time - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
    }

    public static string GenerateTransId(int i)
    {
      string transId = DateTime.Now.ToString("yyyyMMddHHmmss");
      int l = i - 14;
      return transId + CreateRandCode(l);

    }

    public static string CreateRandCode(int codeLen)
    {
      string codeSerial = "1,2,3,4,5,6,7,a,c,d,e,f,h,i,j,k,m,n,p,r,s,t,A,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,U,V,W,X,Y,Z";
      if (codeLen == 0)
      {
        codeLen = 16;
      }
      string[] arr = codeSerial.Split(',');
      string code = "";
      int randValue = -1;
      Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
      for (int i = 0; i < codeLen; i++)
      {
        randValue = rand.Next(0, arr.Length - 1);
        code += arr[randValue];
      }
      return code;
    }

    public static string GetStringFromList(List<string> list)
    {
      StringBuilder sb = new StringBuilder();
      string strReturn;
      if (list.Count > 0)
      {
        foreach (string item in list)
        {
          sb.AppendFormat("'{0}',", item);
        }
        strReturn = sb.ToString(0, sb.Length - 1);
      }
      else
      {
        strReturn = "''";
      }
      return strReturn;
    }
  }

以上就是c# 靜態(tài)類的使用場(chǎng)景的詳細(xì)內(nèi)容,更多關(guān)于c# 靜態(tài)類的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論