如何在c#中返回一个对象数组

时间:2018-02-16 18:33:55

标签: c# arrays object

我有两个类,Program(主类)和Driver。我想要做的是允许用户通过命令行创建一个驱动程序但是我似乎无法打印出阵列。我是c#的新手,编程并不确定这是解决这个问题的最佳方法,所以任何想法都有帮助。感谢

主类

public class Program    
{           
    static void Main(string[] args)
    {
        List<Driver> MyDriver = new List<Driver>();
        for (var a = 0; a < 5; a++)
        {    
            Driver driver = new Driver();                   
            Console.WriteLine("Enter the Drivers Name");
            string driverName = driver.Name(Console.ReadLine());    
            Console.WriteLine("Enter the Drivers Occupation");
            string driverOccupation = driver.Occupation(Console.ReadLine());
            Console.WriteLine("How many claims does this driver have?");                 
            int noOfClaims = driver.Claim(int.Parse(Console.ReadLine()));                    
            Console.WriteLine("Your car details: \n " + driverName + " \n " + driverOccupation + "\n " + noOfClaims);    
            MyDriver.Add(new Driver());                  
        }

        foreach (Driver i in MyDriver)
        {
            Console.WriteLine(" Driver Details  " + MyDriver);
        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

}

驱动程序类

public class Driver  
{
    //Properties
    private string driverName; 
    private string driverOccupation;  
    private DateTime driverDOB; 
    private DateTime claimDate;  
    private int noOfClaims;  

    public Driver()//Default Constructor
    {
        driverName = "";
        driverOccupation = "";
        driverDOB = DateTime.MinValue;// Default value 01/01/0001
        claimDate = DateTime.MinValue;// Default value
        noOfClaims = 0;  
    }

    public Driver(string name , string occupation , DateTime dateOfBirth , DateTime dateOfClaim, int claims)
    {
        driverName = name;
        driverOccupation = occupation;
        driverDOB = dateOfBirth;
        claimDate = dateOfClaim;
        noOfClaims = claims;
    }

    public string Name(string driverName)
    {
        return driverName;
    }

    public string Occupation (string driverOccupation)
    {
        return driverOccupation;         
    }

    public DateTime DateOfBirth(DateTime driverDOB) 
    {
        return driverDOB;       
    }

    public int Claim(int noOfClaims) 
    { 
        return noOfClaims;     
    }

    public override string ToString()
    {
        return String.Format(driverName , driverOccupation , driverDOB , noOfClaims );
    }
}

3 个答案:

答案 0 :(得分:0)

改变这个:

        for (var a = 0; a < 5; a++)
        {

            Driver driver = new Driver();

            Console.WriteLine("Enter the Drivers Name");
            string driverName = driver.Name(Console.ReadLine());

            Console.WriteLine("Enter the Drivers Occupation");
            string driverOccupation = driver.Occupation(Console.ReadLine());



            Console.WriteLine("How many claims does this driver have?");

            int noOfClaims = driver.Claim(int.Parse(Console.ReadLine()));


            Console.WriteLine("Your car details: \n " + driverName + " \n " + driverOccupation + "\n " + noOfClaims);

           MyDriver.Add(new Driver());

        }

到此:

        Driver driver;   
        for (var a = 0; a < 5; a++)
        {



            Console.WriteLine("Enter the Drivers Name");
            string driverName = Console.ReadLine();

            Console.WriteLine("Enter the Drivers Occupation");
            string driverOccupation = Console.ReadLine();



            Console.WriteLine("How many claims does this driver have?");

            int noOfClaims = int.Parse(Console.ReadLine());

            driver = new Driver(driverName, driverOccupation, noOfClaims );

            Console.WriteLine("Your car details: \n " + driverName + " \n " + driverOccupation + "\n " + noOfClaims);

           MyDriver.Add(driver);

        }

并在驱动程序类中添加此

public Driver(string name , string occupation , int claims)
{
    driverName = name;
    driverOccupation = occupation;
    driverDOB = DateTime.MinValue;
    claimDate = DateTime.MinValue;
    noOfClaims = claims;
}

印刷部分应该是这样的:

  foreach (Driver i in MyDriver)
    {
        Console.WriteLine("\n Driver Details  " + i.toString());
    }

答案 1 :(得分:0)

更改您的打印循环:

foreach (Driver i in MyDriver)
{
   Console.WriteLine(" Driver Details  " + i);
}

答案 2 :(得分:0)

此代码:

    foreach (Driver i in MyDriver)
    {
        Console.WriteLine(" Driver Details  " + MyDriver);
    }

应该更改为:

    foreach (Driver driver in MyDriver)
    {
        Console.WriteLine(" Driver Details  " + driver.toString());
    }