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();
}
}
}
我这里用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();
}
}
}
解决方案 »
- 关于VSTO开发Outlook插件的部署
- 什么银行有这样的业务:能从网上浏览到别人给我的汇款金额等即时信息。建行、工商行、...有这样的业务吗?
- 如何检测局域网中所有的SQL Server服务器,并显示所有服务器地址?
- 如何把string “1”,“2”改成“01”,“02”这种
- 关于"由于线程退出或应用程序请求,已放弃 I/O 操作"错误的问题
- 请大家推荐做winform的好用的皮肤控件!
- 很奇怪的问题.让各位高手看看,帮忙解决?
- vs2010 chart控件X轴间距设置问题
- CSDN服务器调整以后,速度奇慢无比,版主门反映一下,如果短时间内没有改善的话,我们只有转到其它论坛了!!
- 请问各位大狭,用NEW创建的对象怎么自己删除!!急急!!
- vs2010 C# 调试的时候如何显示输出具体的数值
- 关于静态类的执行顺序问题
List<int> nullableInt = new List<int>();
nullableInt.Add(5);
直接赋值即可。
形如:
System.Nullable<int> nullableInt = new System.Nullable<int>();//int? nullableInt;
nullableInt = 1;
nullableInt ++;等等
1、他们两个不是一个东西
2、泛型针对于arraylist的优点有,1不用装箱拆箱,提高性能,2类型安全,避免了运行时错误
{
if (nullableInt.HasValue)
{
return nullableInt.Value+value;//不行啊,求指点!!!
}
else
{
return null;
}
}你可以把所有stuct都写一次
public static Nullable<int> Add(this Nullable<int> nullableInt, int value)
{
if (nullableInt.HasValue)
{
return nullableInt.Value + value;
}
else
{
return null;
}
}int? abc = 10;
int? cd = abc.Add(1);
Console.WriteLine(cd);//11
我一直不明白为什么有些人就喜欢把泛型和集合这两个没有一点关联的东西放到一起想问题。
Nullable<T> 是泛型ArrayList是集合(基本淘汰)List<T>是泛型集合 List<object>约等于ArrayList我写的是扩展方法为什么要使用泛型集合呢,因为C#是强类型语言
ArrayList al;
al.add(1); al.add("a") 没有问题,但读出来呢,比如你存,我读,我就不知道第二个是string类型,al[0]+al[1]报错!!!!!!List<int> li;只能存int类型的
li.add(1);li.add("a")编译器和VS不让你这样干
public static class NullableExtend
{
public static Nullable<T> Add<T>(this Nullable<T> nullableInt, Nullable<T> value) where T : struct
{
if (nullableInt.HasValue)
{
dynamic d1 = nullableInt.Value;
dynamic d2 = value; return d1 + d2;
}
else
{
return value;
}
}
}
确实,一说到泛型马上就有人把List<T>翻出来了