码迷,mamicode.com
首页 > 其他好文 > 详细

泛型方法和空值

时间:2014-05-09 03:20:29      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

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

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


测试类(NullableTest)

bubuko.com,布布扣
bubuko.com,布布扣
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
bubuko.com,布布扣

主方法(Main)

bubuko.com,布布扣
bubuko.com,布布扣
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
bubuko.com,布布扣

输出结果

bubuko.com,布布扣

 

2.泛型方法调用和约束

账务处理接口(IAmount)

bubuko.com,布布扣
bubuko.com,布布扣
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
bubuko.com,布布扣

账务类(Amount) 实现接口IAmount

bubuko.com,布布扣
bubuko.com,布布扣
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
bubuko.com,布布扣

账务类(Amount2) 实现接口IAmount

bubuko.com,布布扣
bubuko.com,布布扣
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
bubuko.com,布布扣

计算账务合计类(Algorithm)

bubuko.com,布布扣
bubuko.com,布布扣
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
bubuko.com,布布扣

主方法类

bubuko.com,布布扣
bubuko.com,布布扣
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
bubuko.com,布布扣

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

bubuko.com,布布扣

 

 

 

 

 

 

 

 

 

 

泛型方法和空值,布布扣,bubuko.com

泛型方法和空值

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/zxd543/p/3716387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!