泛型方法和空值

数据库中的数字和编程语言中的数字有显著的不同,数据库中的数字可以为空,C#中的数字不能为空。

int? x = null; //此时x可以为空


测试类(NullableTest)

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

namespace ListMethod
{
    /// <summary>
    /// 可空类型的测试
    /// </summary>
    class NullableTest
    {
        public NullableTest()
        { }

        /// <summary>
        /// 输出测试结果
        /// </summary>
        /// <param name="cur"></param>
        public void WriteTest(int? cur)
        {
            int? x = cur;
            int z = 6;
            if (x == null)
            {
                Console.WriteLine("X is NULL");  
                //空转非空
                Console.WriteLine("-----空转非空-----");
                int y = x ?? 0;  //如果x为null 则y=0; ??合并运算符 或强制类型转换  int y = (int)x
                Console.WriteLine("x为null时y的值{0}:", y);
                //非空转空
                Console.WriteLine("-----非空转空-----");
                x = z;  //直接复制
                Console.WriteLine("x为null时z的值赋给x后x={0}:", x);
            }
            else if(x<0)
            {
                Console.WriteLine("X is smaller than 0");
                
            }

        }
    }
}
View Code

主方法(Main)

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

namespace ListMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            NullableTest test = new NullableTest();
            test.WriteTest(null);
            test.WriteTest(-10);
        }
    }
}
View Code

输出结果

 

2.泛型方法调用和约束

账务处理接口(IAmount)

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

namespace List_Method
{
    public interface IAmount
    {
        decimal Balance { get; }
        string Name { get; }
    }
}
View Code

账务类(Amount) 实现接口IAmount

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

namespace List_Method
{
    class Amount:IAmount
    {
        public string Name
        {
            get;
            private set;
        }
        public decimal Balance
        {
            get;
            private set;
        }

        public Amount(string name, decimal balance)
        {
            this.Name = name;
            this.Balance = balance;
        }
    }
}
View Code

账务类(Amount2) 实现接口IAmount

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

namespace List_Method
{
    class Amount1:IAmount
    {
        public string Name
        {
            get;
            private set;
        }
        public decimal Balance
        {
            get;
            private set;
        }

        public Amount1(string name, decimal balance)
        {
            this.Name = name;
            this.Balance = balance;
        }
    }
}
View Code

计算账务合计类(Algorithm)

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

namespace List_Method
{
    public static class Algorithm
    {
        /// <summary>
        /// (泛型约束)此方法的优势是TAccount可以为任何类
        /// </summary>
        /// <typeparam name="TAccount"></typeparam>
        /// <param name="sourse"></param>
        /// <returns></returns>
        public static decimal AccumulateSimple<TAccount>(IEnumerable<TAccount> sourse)
            where TAccount : IAmount  
        {
            decimal sum = 0;
            foreach (TAccount account in sourse)
            {
                sum += account.Balance;
            }
            return sum;
        }
    }
}
View Code

主方法类

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

namespace List_Method
{
    class Program
    {
        static void Main(string[] args)
        {
            var account = new List<Amount>()
            {
                new Amount("zhang", 1500),
                new Amount("wang",1600),
                new Amount("li", 1700),
                new Amount("zhao", 1800),
            };
            decimal amount = Algorithm.AccumulateSimple(account);  //调用泛型的方法
            Console.WriteLine("Amount合计为:{0}", amount);

            //账务类二
            var account1 = new List<Amount1>()
            {
                new Amount1("zhang", 1500),
                new Amount1("wang",1600),
                new Amount1("li", 1700),
                new Amount1("zhao", 1800),

                new Amount1("other",2000),
            };
            decimal amount1 = Algorithm.AccumulateSimple(account1);  //调用泛型的方法
            Console.WriteLine("Amount2合计为:{0}", amount1);

        }
    }
}
View Code

输出结果:(本例泛型同时计算两个类的账务,可以多个)

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/zxd543/p/3716387.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值