zoukankan      html  css  js  c++  java
  • C#中对象的输出

    C#中对象的输出

    2013-08-09 17:10 by TangPro, 111 阅读, 0 评论, 收藏编辑

    假设有个Costmer类如下:

    复制代码
        class Costmer
        {
            public string Id { get; set; }
            public string City { get; set; }
            public string Country { get; set; }
            public string Region { get; set; }
            public string Sales { get; set; }
    
            public Costmer(string id,string city,string country,string region,string sales)//构造函数
            {
                Id = id;
                City = city;
                Country = country;
                Region = region;
                Sales = sales;  
            }
            public override string ToString()//重写ToString()方法,以便于输出
            {
                return "ID:" + Id + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
            }
        }
    复制代码

    创建两个 Costmer 类的实例,然后分别输出:

    复制代码
            Costmer c = new Costmer("01", "乐山", "中国", "四川", "999");
                Costmer d = new Costmer("02", "成都", "中国", "四川", "123");
                Console.WriteLine(c);
                Console.WriteLine(d);
    复制代码

    结果如下:

    我们也可以不完全输出类的值,只输出一部分信息,比如我们这里只输出:ID,City,Country的值:

    则代码如下:

    复制代码
                Costmer c = new Costmer("01", "乐山", "中国", "四川", "999");
                Costmer d = new Costmer("02", "成都", "中国", "四川", "123");
                Console.WriteLine("ID:{0} City={1} Country={2}",c.Id,c.City,c.Country);
                Console.WriteLine("ID:{0} City={1} Country={2}", d.Id, d.City, d.Country);
    复制代码

    结果:

    注意:假若 Costmer 类中没有对 ToString()方法进行重写,则输出结果为:

    但是当有多个对象的时候,我们需要将这些对象放在集合中,然后再输出,为此,我们需要创建一个泛型集合

    创建一个Costmer类的泛型集合,并向集合中添加 对象

    复制代码
            List<Costmer> costmers = new List<Costmer>();
                costmers.Add(new Costmer("01","乐山","中国","四川","999"));
                costmers.Add(new Costmer("02","成都","中国","四川","123"));
                costmers.Add(new Costmer("03", "重庆", "中国", "重庆", "1234"));
    复制代码

    输出对象的值:

            for (int i = 0; i < costmers.Count; i++)
                {
                    Console.WriteLine(costmers[i]);
                }

    结果:

    同理也可以只输出对象一部分的值:  在"."后加入对象需要输出的属性值即可

            for (int i = 0; i < costmers.Count; i++)
                {
                    Console.WriteLine(costmers[i].City);
                }

    使用Linq输出对象:

    复制代码
           var queryResults = from n in costmers where n.Region == "四川" select n;  //注意 是两个=号,这是不是赋值,是判断
    
                foreach (var item in queryResults)
                {
                    Console.WriteLine(item.ToString());
                }
    复制代码

    结果:

     
     
    分类: C#基础
  • 相关阅读:
    iOS --- Touch ID指纹解锁
    Android手势滑动Tab
    Android:自定义Dialog
    Android:视频(VideoView/MediaPlayer)
    Android用户界面开发:Fragment
    Android用户界面开发:TabHost
    Android:Animation
    WallPaper
    linux网络设备驱动
    Android通讯:短信
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3248895.html
Copyright © 2011-2022 走看看