第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

靜態(tài)列表顯示為空

靜態(tài)列表顯示為空

C#
慕尼黑的夜晚無繁華 2022-10-23 15:07:55
我有一個名為常量的靜態(tài)類。public static class Constants{    public static string sampleString= "";    public static List<int> sampleList= new List<int> {1,2,3};}如果我在外面調(diào)用我的靜態(tài)列表: Constants.sampleList它會給我一個空異常,但Constants.sampleString可以毫無問題地調(diào)用。我在這里錯過了什么嗎?
查看完整描述

5 回答

?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗 獲得超9個贊

不確定這是否與我們的情況相同,但我們有一個類可以從配置中訪問密鑰

string sampleString = WebConfigurationManager.AppSettings["SampleString"]

由于一些合并問題,示例字符串已從我們的 web.config 中刪除。當(dāng)您訪問變量下面的類中的任何變量時,會發(fā)生空指針錯誤。

在配置中添加密鑰解決了這個問題。


查看完整回答
反對 回復(fù) 2022-10-23
?
湖上湖

TA貢獻(xiàn)2003條經(jīng)驗 獲得超2個贊

當(dāng)我運行此代碼時:


void Main()

{

    Console.WriteLine(Constants.sampleList.Contains(1));

}


public static class Constants

{

    public static string sampleString= "";

    public static List<int> sampleList= new List<int> {1,2,3};

}

我進(jìn)入True控制臺。您需要提供演示您所面臨問題的代碼。


查看完整回答
反對 回復(fù) 2022-10-23
?
largeQ

TA貢獻(xiàn)2039條經(jīng)驗 獲得超8個贊

如果您readonly在方法中添加關(guān)鍵字。該方法只能在構(gòu)造函數(shù)內(nèi)部或在屬性聲明階段進(jìn)行實例化。

public static readonly List<int> sampleList= new List<int> {1,2,3};

如果您嘗試重新初始化或?qū)?進(jìn)行新分配sampleList,C# 編譯器會給您編譯器錯誤。

更好地使用屬性

public static readonly List<int> SampleList {get; set;} = new List<int> {1,2,3};


查看完整回答
反對 回復(fù) 2022-10-23
?
白衣非少年

TA貢獻(xiàn)1155條經(jīng)驗 獲得超0個贊

沒有奇跡,如果sampleList是null(并且您拋出了異常),那么您null在某處分配給它。盡量不要暴露 易受攻擊的public 字段:Constants


  public static class Constants

  {

      public static string sampleString = "";

      public static List<int> sampleList = new List<int> {1,2,3};

  }


  ...


  Constants.sampleList = null;   // We can easly assign null


  ...


  Constants.sampleList.Add(123); // <- And have an unxpected exception

但是要么把它們變成屬性:


  public static class Constants

  {

      private static string s_SampleString = "";

      private static List<int> s_SampleList = new List<int> {1,2,3}; 


      public static string sampleString {

        get {return s_SampleString;}

        set {s_SampleString = value ?? "";}

      }


      public static List<int> sampleList {

        get {return s_SampleList;}

        // I doubt you actually want set here

        set {s_SampleList = value ?? new List<int>();}  

      }

  }

或者,至少,將字段標(biāo)記為readonly(您只能分配一次):


  public static class Constants

  {

      private static string s_SampleString = "";


      public static string sampleString {

        get {return s_SampleString;}

        set {s_SampleString = value ?? "";}

      }


      // Manipulate with me, but not reassign

      public static readonly List<int> sampleList = new List<int> {1,2,3}; 

  }

無論哪種情況,您仍然可以使用列表進(jìn)行操作:


  Constants.sampleList.Add(4);

  Constants.sampleList.RemoveAt(0);

但是您可以避免分配null給列表:將分配空列表(帶有屬性的代碼),或者您將遇到編譯時錯誤(帶有 的代碼readonly)


查看完整回答
反對 回復(fù) 2022-10-23
?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗 獲得超6個贊

在我的例子中,我private static readonly Dictionary<byte, string>在一個結(jié)構(gòu)內(nèi)部定義了一個來保存預(yù)定義的常量。這通??梢哉9ぷ鳎俏疫€在我的結(jié)構(gòu)中定義了一個 MinValue 來表示一條數(shù)據(jù)的最小值。以這種方式完成時,靜態(tài)字典未初始化,除非在靜態(tài) MinValue 屬性上方定義。我可能對編譯器的要求太多了,應(yīng)該對其進(jìn)行重構(gòu)。


在大型結(jié)構(gòu)中很難診斷,因為我沒想到 C# 會出現(xiàn)這種行為。重現(xiàn)示例:


public struct MyStruct

{

    public string Str;


    public static readonly MyStruct MinValue = new MyStruct(0);


    public MyStruct(byte val)

    {

        Str = _predefinedValues[val]; // null reference exception

    }


    private static readonly Dictionary<byte, string> _predefinedValues = new Dictionary<byte, string>()

    {

        {0x00, "test 1"},

        {0x01, "test 2"},

        {0x02, "test 3"},

    };


}

這里的解決方案是雙重的:

  1. 不調(diào)用結(jié)構(gòu)上的任何構(gòu)造函數(shù)并在不訪問_predefinedValues列表的情況下顯式設(shè)置每個字段。

  2. 或者,將列表向上移動到 MinValue 字段聲明之上實際上也可以修復(fù)它(奇怪吧?)

我懷疑在嘗試分配它時堆棧上發(fā)生了一些奇怪的事情,這可能是一件奇怪的事情。



查看完整回答
反對 回復(fù) 2022-10-23
  • 5 回答
  • 0 關(guān)注
  • 157 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號