开发者

How can I create a list and look up a value in C#

开发者 https://www.devze.com 2023-03-25 17:16 出处:网络
I would like to implement some kind of a look up in C# but I am not sure how to do this. I have a variable that can have a value of between 0 and 9. For each variable there will be a text string tha

I would like to implement some kind of a look up in C# but I am not sure how to do this.

I have a variable that can have a value of between 0 and 9. For each variable there will be a text string that goes with it.

I guess I need to set up some kind of list.

A really simple example on how I could access the list and how I could populate the开发者_运维技巧 list would be much appreciated.

I tried the following:

public static class RefData
{
    public static Dictionary<int, string> dict = new Dictionary<int, string>();
    dict.Add(0, "This Text");
    dict.Add(3, "That Text");
    dict.Add(4, "More Text");
}

but it gives an error message saying "can't resolve symbol dict on the lines with the dict.Add


This is an answer to the updated question. The problem with your current code is that you're trying to execute statements without them being inside a method, property, constructor etc. You can't do that. There are two obvious options here. First, you can use a method to build your dictionary:

public static class RefData
{
    private static readonly Dictionary<int, string> dict = BuildDictionary();

    private static Dictionary<int, string> BuildDictioary()
    {
        Dictionary<int, string> ret = new Dictionary<int, string>();
        ret.Add(0, "This Text");
        ret.Add(3, "That Text");
        ret.Add(4, "More Text");
        return ret;
    }
}

The other is to use a collection initializer if you're using C# 3 or higher:

public static class RefData
{
    private static readonly Dictionary<int, string> dict = 
        new Dictionary<int, string>
    {
        { 0, "This Text" },
        { 3, "That Text" },
        { 4, "More Text" }
    };
}

Note how in both cases I've made the variable private and read-only. You should almost never expose fields publicly - particularly mutable ones. You'll probably want to provide appropriate methods which act on the dictionary - and remember that Dictionary is not thread-safe.


You're trying to put code inside of a class definition. You need to move your dict.Add() calls either in a method or a static constructor. Something like:

public static class RefData
{
    public static Dictionary<int, string> dict = new Dictionary<int, string>();

    static Refdata() //this could also be: public static void PopulateDictionary()
    {
        dict.Add(0, "This Text");
        dict.Add(3, "That Text");
        dict.Add(4, "More Text"); 
    }
}

You can do that if you want to fill it with default data, I suppose, you can always add or remove keys from it later from other parts of your code, since its a public static member.


A generic dictionary. Something like:

 Dictionary<int, string> dict = new Dictionary<int, string>()

The first type, int is your key. Each key has to be unique, and will have a string value.

If you did something like this:

 dict.Add(0, "This Text");
 dict.Add(3, "That Text");
 dict.Add(4, "More Text");

you could look up the value like this:

 string value = dict[3];  //returns "That Text"

If a key already exists in the dictionary, the add will fail. As in you can't do this twice:

 dict.Add(3, "That Text");
 dict.Add(3, "More Text");

As I said before, the key (in this case your int) has to be unique. You can reassign the value by doing

 dict[3] = "New Text"

The great thing about a generic dictionary, is that it can hold any type you want! The definition of the dictionary is Dictionary<Tkey, TValue>, you get to specify the types, and you can set them to whatever you want.


You can use the Dictionary class in System.Collections.Generic

Dictionary<byte, string> dct = new Dictionary<byte, string>();

You can add values to the dictionary like this

dct.Add(0, "Some Value");

you can access the values in dictionary like this

string val = dct[0];


I have a variable that can have a value of between 0 and 9. For each variable there will be a text string that goes with it. 

This means that you need a dictionary. You can use many specialized dictionaries in .NET. But my recommendation is to use:

Dictionary<int, string> list = new Dictionary<int, string>();

To check if the list already contains something or not, simply use:

list.ContainsKey(someIntHere);

To get the string value of the specified key, simply use:

string value = list[key];


The dictionary class is your friend here.

e.g.

var textByNumber = new Dictionary<int, string>();    
textByNumber[0] = "Alice";    
textByNumber[1] = "Bob";

And to retrive a value, you can go:

var text = textByNumber[1];
0

精彩评论

暂无评论...
验证码 换一张
取 消