C# Func 和 Action

这段代码展示了C#中的日期时间操作,如添加月份,以及创建和修改记录类对象的属性。此外,还涉及了条件判断,字符串检查,以及各种函数和表达式的用法,包括Func和Action的定义与应用。
public class Tests
    {
        [Test]
        public void Test2()
        {
            DateTime startDate = new DateTime(2022, 10, 1, 0, 0, 0);
            DateTime endDate = startDate.AddMonths(1).AddSeconds(-1);

            var v = Environment.Version;

            var s1 = new Sword2() { Damage = 10000, Durability = 10000 };

            var s2 = s1 with { Damage = 10001 };

            var s3 = new Sword2() { Damage = 10000, Durability = 10000 };

            Sword s4 = new(100, 100);

            if (s1.Aggressivity is null or { Length: 0 })
            {
                Console.WriteLine($"{nameof(s1.Aggressivity)} is IsNullOrEmpty");
            }

            if (s1.Aggressivity is not null and { Length: > 0 })
            {
                if (s1.Aggressivity[0] is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z') or '.')
                {
                }
            }

            switch (s1.Damage)
            {

                case >= 0 and < 100:
                    break;
                case >= 100 and < 1000:
                    break;
                case >= 1000 and < 100001:
                    break;
            }

            Expression<Func<int, bool>> expression1 = static i => i > 1;

            Func<string, string> exp = (x) => x;
            var result = exp("Hello world");

            Func<int, int, int> exp1 = (x, y) => x * y;
            var result1 = exp1(0, 1);
            result1 = exp1(1, 10);

            Action<int, int, List<int>> exp2 = (x, y, z) => { z.Add(x + y); };

            var list = new List<int>();
            exp2(1, 2, list);
        }

        record class Sword(int Damage, int Durability);

        record class Sword2
        {
            public int Damage { get; set; }
            public int Durability { get; set; }
            public string Aggressivity { get; set; }
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值