日期:2014-05-17  浏览次数:20991 次

System.Nullable<int> 怎么添加成员
System.Nullable 和 ArrayList是类似的吧? ArrayList 已经可以追加任何类型的变量,又为啥产生个泛型。
我这里用Int试了下,输入“.”符号之后没有“add”函数,不知道这怎么追加变量啊

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Nullable<int> nullableInt = new System.Nullable<int>();
            nullableInt.//这里没有add
            Console.WriteLine(nullableInt.Value);
            Console.ReadKey();
        }
    }
}

------解决方案--------------------
nullableInt=int
------解决方案--------------------
null
------解决方案--------------------
单个变量怎么能添加成员?
List<int> nullableInt = new List<int>();
            nullableInt.Add(5);

------解决方案--------------------
又不是 集合 ,是可空类型,是值类型
------解决方案--------------------
“Nullable<int>”等价于“int?”
直接赋值即可。
形如:
            System.Nullable<int> nullableInt = new System.Nullable<int>();//int? nullableInt;
            nullableInt = 1;
            nullableInt ++;

等等
------解决方案--------------------
两点
1、他们两个不是一个东西
2、泛型针对于arraylist的优点有,1不用装箱拆箱,提高性能,2类型安全,避免了运行时错误
------解决方案--------------------
        public static Nullable<T> Add<T>(this Nullable<T> nullableInt, T value) where T: struct
        {
            if (nullableInt.HasValue)
            {
                return nullableInt.Value+value;//不行啊,求指点!!!
            }
            else
            {
                return null;
            }
        }

你可以把所有stuct都写一次
        public static Nullable<int> Add(this Nullable<int> nullableInt, int value)