List:对象 Equals、Contains

放肆的年华 提交于 2020-04-07 07:41:35
ylbtech-List:对象 Equals、Contains

 

1.返回顶部
1.1、
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Person dal1 = new Person() { Id = 101, Name = "rain" , Age=10 };
            Person dal2 = new Person() { Id = 101, Name = "rain" , Age=10.0M};
            Person dal3 = new Person() { Id = 101, Name = "rain" };
            Person dal4 = new Person() { Id = 101, Name = "rain" };
            Person dal5 = new Person() { Id = 101 };

            List<Person> dals1 = new List<Person>();
            dals1.Add(dal1);
            dals1.Add(dal3);

            Console.WriteLine("{0}", dal3.Equals(dal3));
            Console.WriteLine("{0}", dal3.Equals(dal4));
            Console.WriteLine("----------------");

            Console.WriteLine("{0}", dals1.Contains(dal1));
            Console.WriteLine("{0}", dals1.Contains(dal2));
            Console.WriteLine("{0}", dals1.Contains(dal3));
            Console.WriteLine("----------------");
            Console.WriteLine("{0}", dals1.Contains(new Person() { Id = 101, Name = "rain" }));
            Console.WriteLine("----------------");
            Console.WriteLine("{0}", dals1.Contains(dal4));
            Console.WriteLine("{0}", dals1.Contains(dal5));
            Console.WriteLine("----------------");
            Console.WriteLine("{0}", dals1.Remove(dal1));
            Console.WriteLine("{0}", dals1.Contains(dal1));
            Console.Read();

        }
    }
    class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Age { get; set; }
    }
}
1.2、input
True
False
----------------
True
False
True
----------------
False
----------------
False
False
----------------
True
False
2、
2.返回顶部
 
3.返回顶部
 
4.返回顶部
 
5.返回顶部
 
 
6.返回顶部
 
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!