简体   繁体   中英

C# Questions Regarding Lists?

I have two classes: consumableItems.cs and items.cs So basically, all I have to do is inherit the properties of items.cs to consumableItems.cs

Here's what I done so far:

class Item
{
    public List<string> itemNames = new List<string>();
    public List<int> itemEffects = new List<int>();
}


class consumableItems : Item 
{
    new public List<string> itemNames = new List<string>() { "Apple", "Orange", "Grapes" };
    new public List<int> itemEffects = new List<int>() { 15, 30, 40 };
}

What I want to achieve is, whenever I type "Apple", the console window shows both "Apple" and "15"; same for when I type "Orange", the console window shows both "Orange" and "30". Any ideas? Sorry, just started C# programming and I'm getting lost. > < Oh, and last question, is the way I inherit correct? :/ Thanks. ^ ^

If you just starting with C# what about changing from List to Dictionnary ?

a Dictionary would give you what you want.

With two lists, you have to loop over the first one to find the index and then access the second list with the index. Be careful with Exception in that case.

Regarding inheritance, you should check for (public|private|Etc...) and maybe look for Interfaces and Abstract

You are re-inventing the wheel and making life hard. Just use a dictionary:

var items = new Dictionary<string, int>
{
    { "Apple", 15 },
    { "Orange", 30 },
    { "Grapes", 40 }
};

Console.WriteLine("Apple = {0}", items["Apple"]);

I propose you to define a class

class Item {
  public string Name { get; set;}
  public int Effect { get; set;}
}

And then use a single List<Item> instead of trying to map between the two lists. You could override the ToString() method of the class for your Console output.

Use Dictionary like in example below:

  class Program2
    {
        class ConsumableItems
        {
            new public List<string> itemNames = new List<string>() { "Apple", "Orange", "Grapes" };
            new public List<int> itemEffects = new List<int>() { 15, 30, 40 };

            public Dictionary<string, int> values = new Dictionary<string, int>()
            {
                {"Apple", 15},
                {"Orange", 30},
                {"Grapes", 40}
            };
        }

        static void Main()
        {
            ConsumableItems items = new ConsumableItems();

            string key = Console.ReadLine();

            Console.WriteLine("\n\n\n");

            Console.WriteLine(key + "   " + items.values[key]);

            Console.ReadKey();
        }
    }

在此处输入图片说明

You can use Dictionary instead of List ,

public Dictionary<string, int> FruitValues = new Dictionary<string, int>()
            {
                {"Apple", 15},
                {"Orange", 30},
                {"Grapes", 40}
            }; 

Console.WriteLine("Apple Value is {0}", FruitValues["Apple"]);

Same business problem can easily be solved by using any collection of Key-Value pair.. I mean using dictionary like:

public Dictionary<string, int> FruitsEffect= new Dictionary<string, int>()
FruitsEffect.Add("FruitsName",25);

The Dictionary has pairs of keys and values.Dictionary is used with different elements. We specify its key type and its value type (string, int). Populate the dictionary and get the value by key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM