如何在 C# 中动态获取 LIST<> 中多个字段的用户输入,而不是像我在下面的代码中那样进行硬编码?

How to take user input of multiples fields in LIST<> in C# dynamically not hard coded like i did the code below?

using System;
using System.Collections.Generic;

namespace Branches
{
    public class Branch
    {
        public int Id { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }  

        public Branch() { }

        public Branch(int id, string code, string name)
        {
            Id = id;
            Code = code;
            Name = name;
        }
    }

    class Program
    {
        static void Main()
        {
            List<Branch> br = new List<Branch>(){
                new Branch(){ Id=401, Code="SBI120800", Name="Chembur West" },
                new Branch(){ Id=402, Code="SBI120700", Name="Chembur East" },
                new Branch(){ Id=403, Code="SBI120900", Name="Govandi West" },
                new Branch(){ Id=404, Code="SBI120500", Name="Govandi East" },
                new Branch(){ Id=405, Code="SBI120400", Name="Andheri West" },
                new Branch(){ Id=406, Code="SBI120300", Name="Andheri East" },
            };

            foreach (var branches  in br)
            {
                Console.WriteLine(branches.Name);
            }
        }
    }
}

你可以这样做:

List<Branch> br = new List<Branch>();
while(true) 
{
 Console.WriteLine("Please provide Id")
 var id =  Console.ReadLine();
 ... // Ask other questions 
 br.Add( new Branch { Id =id, ...});
 Console.WriteLine("Would you like to add another branch? Y/N");
 if(Console.ReadKey().ToLower() == 'n') break;
}

剪裁空格的片段,使用逗号作为各个字段之间的分隔符和不同 Branch 对象之间的更新,检查重复的 ID、代码和名称,并根据我在您上面的代码中看到的格式检查代码格式(SBIxxxxxx):

// This requires LINQ.
List<Branch> br = new List<Branch>(); // You can replace 'new List<Branch>()' with 'new()' if you're using C# 9.0 or newer.
while (true)
{
    Console.WriteLine("Enter the ID, code, and name of the branch, seperated by commas, or enter an empty string to finish:");
    string line = Console.ReadLine();
    if (line == String.Empty)
        break;
    string[] entry = line.Split(',');
    entry[0] = entry[0].Trim();
    entry[1] = entry[1].Trim();
    entry[2] = entry[2].Trim();
    entry[1] = entry[1].ToUpper();
    if (entry.Length != 3)
        Console.WriteLine("Incorrect number of arguments.");
    else if (!Int32.TryParse(entry[0], out int id))
        Console.WriteLine("Invalid ID entered.");
    else if (entry[2] == String.Empty)
        Console.WriteLine("Name must not be blank.");
    else if (!(entry[1].StartsWith("SBI") && Int32.TryParse(entry[1].Remove(0, 3), out int code) && code >= 100000 && code <= 999999))
        Console.WriteLine("Invalid code format.");
    else if ((from e in br
                where ((id == e.Id)
                    || (entry[1] == e.Code)
                    || (entry[2] == e.Name))
                select e).Count() > 0)
        Console.WriteLine("Duplicate ID, code, or name entered.");
    else
        br.Add(new Branch(id, entry[1], entry[2])); // Again, you can replace 'new Branch()' with 'new()' if you're using C# 9.0 or newer.
}
foreach (Branch b in br)
    Console.WriteLine(b.Name);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();

如果您发现此代码存在错误,请报告它们。