what is the difference between list<> and dictionary<> in c#

前端 未结 5 2203
孤独总比滥情好
孤独总比滥情好 2020-12-08 16:00

I have a strange doubt regarding list and dictionary in c#

In a list we add items to list by using the following method

using System.Collections.Gen         


        
5条回答
  •  难免孤独
    2020-12-08 16:34

    I have a class library which accesses a variety of T-sql sprocs; each sproc returns a single row but varying columns. I needed a general purpose solution for an retrieving the values and Dictionary<> provided a much cleaner solution than List<>.

    The class common to all of the wrappers declares

    public Dictionary datadict = new Dictionary();
    

    and

    public Dictionary LoadData(string sproc, string paramName, string paramValue)
    

    Invoking a Reader, the datadict is loaded with

    for (int i = Reader.FieldCount; i != 0; i--)
     {
      datadict.Add(Reader.GetName(i - 1).Trim(), Reader.GetString(i - 1).Trim());
     }
    

    and returns datadict to the calling class which can then retrieve the data much like Reader does; E.g.:

    datadict = myData.LoadData("spGetSSN", "", "");
      ssn1 = datadict["SSN1"];
      ssn2 = datadict["SSN2"];
      ssn3 = datadict["SSN3"];
    

    Much cleaner for me that List<>.

提交回复
热议问题