首页 > 其他分享 >Expression及Equal Demo

Expression及Equal Demo

时间:2023-01-03 21:11:54浏览次数:41  
标签:Body Console string People Demo Equal Equals WriteLine Expression

代码参考1:

using System;
using System.Linq.Expressions;

namespace ExpressionDemo
{
    class People
    {
        public int Id { set; get; }
        public string Name { set; get; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Expression<Func<People, bool>> expression = p => p.Id == 20;
            Func<People, bool> func = expression.Compile();
            bool bResult = func.Invoke(new People()
            {
                Id = 10,
                Name = "张三"
            });
            Console.WriteLine(bResult);

            Expression<Func<int, int>> expression2 = (num) => num + 5;
            Console.WriteLine($"NodeType:{expression2.NodeType}");
            Console.WriteLine($"Body:{expression2.Body}");
            Console.WriteLine($"Body Type: {expression2.Body.GetType()}");
            Console.WriteLine($"Body NodeType: {expression2.Body.NodeType}");
        }
    }
}

 参考代码2:

using System;

namespace EqualSoOnDemo
{
    class People
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public override bool Equals(object obj)
        {
            People p = obj as People;
            if (this.Name == p.Name)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public People(string name)
        {
            this.name = name;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string a = "hello";
            string b = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
            Console.WriteLine(a == b);
            Console.WriteLine(a.Equals(b));
            Console.WriteLine("\n");
            Int32 m = 3;
            Int32 n = 3;
            Console.WriteLine(n == m);
            Console.WriteLine(n.Equals(m));
            Console.WriteLine("\n");
            object g = a;
            object h = b;
            Console.WriteLine(g == h);
            Console.WriteLine(g.Equals(h));
            Console.WriteLine(g.GetHashCode() + "   " + h.GetHashCode());
            Console.WriteLine("\n");
            People p1 = new People("Jimmy");
            People p2 = new People("Jimmy");
            Console.WriteLine("People...");
            Console.WriteLine(p1 == p2);
            Console.WriteLine(p1.Equals(p2));
            Console.WriteLine(p1.GetHashCode() + "  " + p2.GetHashCode());
            Console.WriteLine("\n");
            People p3 = new People("Jimmy");
            People p4 = p3;
            Console.WriteLine(p3 == p4);
            Console.WriteLine(p3.Equals(p4));
        }
    }
}

参考博客:

https://www.cnblogs.com/zhaoshujie/p/15817805.html

标签:Body,Console,string,People,Demo,Equal,Equals,WriteLine,Expression
From: https://www.cnblogs.com/exesoft/p/17023384.html

相关文章