如何使用自定义类型正确初始化变量

问题描述 投票:-1回答:2

这是C#中的代码:

public bool IsNation(string country)
    {
        for (int i = 0; i < Nations.Count; i++)
        {

            if (Nations[i].Name == country)
            {
                return true;
            }
            else { return false; }

        }return true;

    }

在C#中,您必须初始化变量。但是,如果你在下面制作了自己的一个怎么办?

public class WorldMarket
    {
        public WorldMarket()
        {
            Nations = new List<NationBuilder>();
        }

        internal List<NationBuilder> Nations { get; set; }

        public void AddToWorldMarket(NationBuilder nation)
        {
            Nations.Add(nation);
        }

主要思想是从这个结构:

- wm
   - Nations
      - [0]
          - Name "USA"
          - stockpile 
            - [0]
                - Name "Coal"
                - Quantity "quantity"
                - Value "value"
      - [1] //Same as above

找到国家名称“USA”或此结构中的任何名称,其功能是只插入一个名称为字符串的字符串,输出{1或0}或True或False(如果type == bool)。

我的尝试是这个问题中提出的第一个代码。它尝试“移动”结构,并使用此调用查找您输入的名称标签。

IsNation(string country);国家可以是任何字符串输入。

问题如果C#要我用初始值声明每个变量,我该怎么做这个自定义或我可能做的任何自定义类型?

c# database initialization
2个回答
1
投票

在构造函数public WorkdMarket()中初始化变量。见下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            WorldMarket wm = new WorldMarket();
        }
    }
    public class WorldMarket
    {
        internal List<NationBuilder> Nations { get; set; }
        public WorldMarket()
        {
            Nations = new List<NationBuilder>() {
                new NationBuilder() { 
                    name = "USA", 
                    stockPiles = new List<StockPile>() {
                        new StockPile() { name = "Coal", quantity = 2, value = "value"},
                        new StockPile() { name = "Coal", quantity = 2, value = "value"}
                    }
                }
            };      
        }


        public void AddToWorldMarket(NationBuilder nation)
        {
            Nations.Add(nation);
        }
    }
    public class NationBuilder
    {
        public string name { get; set; }
        public List<StockPile> stockPiles { get; set; }
    }
    public class StockPile
    {
        public string name { get; set; }
        public int quantity { get; set; }
        public string value { get; set; }
    }
}

1
投票

您要求的行是:

WorldMarket con = new WorldMarket();

但是,这将初始化为新的WorldMarket对象,该对象尚未预填充值。如果国家是静止的,你可以初始化WorldMarket类中的所有国家

public class WorldMarket 
{
    public WorldMarket() 
    {
         Nations = new List<NationBuilder>() {
            new NationBuilder() { 
                name = "USA",
                ... 
            },
            new NationBuilder() {
                name = "AUS",
                ...
            }
         }
    }
}    

或者,如果您可以在WorldMarket中使用isNation方法,那么这可能会更好。

public class WorldMarket() 
{
    // various class constructor methods


    public int IsNation(string country)
    {
        // you could access Nations directly here
        for (int i = 0; i < Nations.Count; i++)
        {
            if (Nations[i].Name == country)
            {
                return 1;
            }
            // else { return 0; } -- this would exit the loop unnecessarily
        }
        return 0;
    }
}

并且在主程序中的用法将是类似的

program static void Main(string[] args)
    {
        WorldMarket con = new WorldMarket();
        con.AddToWorldMarket(new NationBuilder() {
            name = "USA",
            ... 
        }
        Console.WriteLine(con.IsNation("USA"));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.