C# — Need an IDictionary implementation that will allow a null key

前端 未结 6 1896
时光取名叫无心
时光取名叫无心 2020-12-11 01:20

Basically, I want something like this:

Dictionary dict = new Dictionary();
dict.Add(null, \"Nothing\");
dict.Add(         


        
6条回答
  •  难免孤独
    2020-12-11 01:54

    Does the key literally need to be NULL? The key in the collection works out to be an index. It doesn't make a lot of sense to me to have NULL for an index in a collection.

    Maybe create a new class

    
    public class ObjectEntry
    {
        public object objRef;
        public string desc;
    
        public ObjectEntry(object objectReference)
        {
            objRef = objectReference;
            if (objRef = null) {desc = "Nothing";}
            else {desc = objRef.Description;} //or whatever info you can get from a proper objRef value
        }
    }
    
    newObj = new ObjectEntry(null);
    dict.add(newObj, newObj.desc);
    

提交回复
热议问题