C#Learner在打印列表时需要帮助 [英] C# Learner needing help on printing a list

查看:49
本文介绍了C#Learner在打印列表时需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

抱歉刚开始面向对象,我被困在下面而不是打印出创建的列表中的项目。



Hi Guys,
Sorry just started object orientated and I am stuck on the below not printing out the items in the list created.

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

namespace Lecture01
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Reservation> reservations = new List<Reservation>()
            {
                new Reservation (23,"Steve", "Jobs", 34,61,47),
                new Reservation (54,"Keith", "Elliot", 45,80,23)
            };

            foreach (Reservation r in reservations)
            {
               Console.WriteLine(r);
               Console.Read();
            }

        }
    }



    public class Reservation
    {
        int ResNumber;
        string FirstName;
        string LastName;
        int RoomNum;
        int Rate;
        //DateTime CheckIn;
        int Duration;




        public int resNumber
        {
            get { return ResNumber; }
            set { ResNumber = value; }
        }
        public string firstName
        {
            get { return FirstName; }

        }
        public string lastName

        {
            get { return LastName; }
        }
        public int roomNum
        {
            get { return RoomNum; }
            set { RoomNum = value; }
        }
        public int rate
        {
            get { return Rate; }
            set { Rate = value; }
        }
        public int duration
        {
            get { return Duration; }
            set { Duration = value; }
        }


        public Reservation(int ResNumber, string FirstName, string LastName, int RoomNum, int Rate, int Duration)
        {
            this.ResNumber = ResNumber;
            this.FirstName = FirstName;
            this.LastName = LastName;
            this.RoomNum = RoomNum;
            this.Rate = Rate;
            this.Duration = Duration;

        }



    }
}

推荐答案

有两种方法可以使它工作。第一个是Ryan在他对你的问题的评论中提到的:



There are two methods to make it work. First one as Ryan has mentioned in his comment to your question:

            List<reservation> reservations = new List<reservation>()
            {
                new Reservation (23,"Steve", "Jobs", 34,61,47),
                new Reservation (54,"Keith", "Elliot", 45,80,23)
            };
 
            foreach (Reservation r in reservations)
            {
               // considering first parameter was for Age member
               Console.WriteLine(r.Age); 
               Console.Read();
            }
</reservation></reservation>





但是,还有另一种方法可以解决这个问题。这是通过覆盖对象 ToString 函数。每次尝试打印对象时都会调用ToString函数,其中。对象的唯一字符串表示形式是其完整类型名称。例如,你的Program对象如果打印





However, there is another method to overcome this. That is by overriding the ToString function of the object. Each time you try to print the object the ToString function is called, in which. The only string representation of the object is its full type name. For example, your Program object if printed

Console.WriteLine(new Program()); 





会打印 Lecture01.Program 。这是对象的字符串表示形式。要解决这个问题,你需要覆盖基本ToString函数并为每个对象实例返回自己的字符串。



记住 ToString是一个实例函数





Would print Lecture01.Program. That is the string representation of your objects. To overcome this, you need to override the base ToString function and return your own string for each of the object instance.

Remember: ToString is an instance function.

class Reservation {
   // Define members
   int ResNumber;
   string FirstName;
   string LastName;
   int RoomNum;
   int Rate;
   //DateTime CheckIn;
   int Duration;
}





现在可以编辑被调用的函数,





Now the function that gets called can be editted out,

public override string ToString() {
   return string.Format("Property1: {0}, Property2: {1}, Property3: {2}, Property4: {3}", Property1, Property2, Property3, Property4); 
}

// For keeping things simple I did not write all the lengthy function. 





记住 string.Format 比连接多个字符串更好。



如果您只需要获得一个属性,您可以随时通过object.Membername调用成员。但是,如果要打印整个对象。然后这将是非常有用的,因为每个对象都将被转换为地下表示。



Remember: string.Format is better than concatenating multiple strings.

In case where you need to get only one property, you can always call a member by object.Membername. However, in cases where you want to print the entire object. Then this would be a lot helpful as every object would be converted to this representation underground.


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

namespace ConsoleApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Lecture01
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<reservation> reservations = new List<reservation>()
            {
                new Reservation (23,"Steve", "Jobs", 34,61,47),
                new Reservation (54,"Keith", "Elliot", 45,80,23)
            };

                foreach (Reservation r in reservations)
                {
                    Console.WriteLine("Reservation Number:{0}",r.resNumber);
                    Console.WriteLine("First Name:{0}", r.firstName);
                    Console.WriteLine("Last Name:{0}", r.lastName);
                    Console.WriteLine("Room Number:{0}", r.roomNum);
                    Console.WriteLine("Rate:{0}", r.rate);
                    Console.WriteLine("Duration:{0}", r.duration);
                    Console.Read();
                }

            }
        }



        public class Reservation
        {
            int ResNumber;
            string FirstName;
            string LastName;
            int RoomNum;
            int Rate;
            //DateTime CheckIn;
            int Duration;




            public int resNumber
            {
                get { return ResNumber; }
                set { ResNumber = value; }
            }
            public string firstName
            {
                get { return FirstName; }

            }
            public string lastName
            {
                get { return LastName; }
            }
            public int roomNum
            {
                get { return RoomNum; }
                set { RoomNum = value; }
            }
            public int rate
            {
                get { return Rate; }
                set { Rate = value; }
            }
            public int duration
            {
                get { return Duration; }
                set { Duration = value; }
            }


            public Reservation(int ResNumber, string FirstName, string LastName, int RoomNum, int Rate, int Duration)
            {
                this.ResNumber = ResNumber;
                this.FirstName = FirstName;
                this.LastName = LastName;
                this.RoomNum = RoomNum;
                this.Rate = Rate;
                this.Duration = Duration;

            }



        }
    }
}


这篇关于C#Learner在打印列表时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆