给梦一个奔跑的方向!
PDF Print E-mail
User Rating: / 0
PoorBest 
Written by xlingfairy
Thursday, 26 March 2009 17:15
Generic type, Nullable Types, HasValue , KeyValuePair , default , where
 
Generic type 泛型
最初接触这个东东是在搞 java 的时候,可惜还没弄懂它就没在搞 java 了。

Nullable Types
一个int 类型的变量是不能被赋值为 null的,如:
int b = null;
是错误的。但是用 System.Nullable 可以使它为 null
System.Nullable<int> a = new System.Nullable<int>();

System.Nullable<int> a = null;

int? a = null;
之后,可以用 a = null, a.HasValue;
如果 a = null, a.HasValue = true.注,hasValue 不是方法。
Nullable 不能用在引用类型的变量上。

注意:
int? a = 2;
int b = a * 2;
会提示错误。
int? a = 2;
int? b = a * 2;//b 也是一个 Nullable 的变量。
才是正确的。
或者:
int? a = 2;
int b = (int)a * 2;//类型转换。
或者:
int? a = null;
int b= a * 2 ?? 0; //如果 a 是null,给 b 一个默认值 0
或者:
int? a= null;
int? b = a * 2 ?? 0;
 
List<Program> ps = new List<Program>();
ps.Add() 等 方法的参数只能为 Program 或 Program 的子类型。
class C : List<A>
 
KeyValuePair
 
            Dictionary<string, int> dics = new Dictionary<string, int>();
            dics.Add("xling",56);
            dics.Add("werewi",65);
            foreach (int val in dics.Values) {
                Console.WriteLine(val);
            }
            foreach (string name in dics.Keys) {
                Console.WriteLine(name);
            }
            foreach (KeyValuePair<string, int> kv in dics) {
                Console.WriteLine("{0} {1}",kv.Key,kv.Value);
            }
 
    class D<T1, T2, T3> {
        public bool Compare(T1 obj1,T2 obj2) {
            obj2 = null;//编译错误,不能将 Null 转换为类型形参“T2”,因为它可能是不可以为 null 值的类型。请考虑改用“default(T2)”。
            //obj2 = default(T2);//???????????? 
            if (obj1 == obj2) // 编译时,这里会报错。但是 obj1 == null 并不报错。
                return true;
            else
                return false;
        }
    }
default(T2)  int/ float/ double 等 的默认值是 0 , bool 的默认值是 False
 
泛型的约束:where
 
类:
class D<T1, T2, T3>{}
中,T1,T2,T3可以是任意类型,
 
class D<T1, T2, T3> where T1: A where T2: B where T3: C{}
T1 只能是 A类型,T2只能是B类型,依此类推。

可用的约束如下:
struct 
 Type must be a value type.
 In a class that requires value types to function, for example where a member variable of type T being 0 means something 
 
class 
 Type must be a reference type.
 In a class that requires reference types to function, for example where a member variable of type T being null means something 
 
base class 
 Type must be, or inherit from, base class.
 In a class that requires certain baseline functionality inherited from base class in order to function 
 
interface 
 Type must be, or implement, interface.
 In a class that requires certain baseline functionality exposed by interface in order to function 
 
new() 
 Type must have a public, parameterless constructor.
 In a class where you need to be able to instantiate variables of type T, perhaps in a constructor
 
但是没看懂上面说的意思:
    struct Man {
        public string name,sex;
        public int age;
    }
 。。。
class D<T1 , T2> where T1: A where T2: Man{}
提示:
错误“ConsoleApplication4.Man”不是有效的约束。作为约束使用的类型必须是接口、非密封类或类型形参。
 
正确的写法:
class D<T1 , T2> where T1: A where T2: new(){}
class D<T1 , T2> where T1: A where T2: IA{}
class D<T1 , T2> where T1: A where T2: struct{}
 
 

Add comment


Security code
Refresh

Popular Contents

Recommend

Site Info

Members : 1
Content : 130
Web Links : 7
Content View Hits : 99622

Links