请问怎么写一个为的!=和==
public static bool operator !=(var a,var b){
//怎么判断是否为空值???
} --------------------编程问答--------------------
class stu
{
public int id { get; set; }
public static bool operator !=(stu a, stu b)
{
return a.id != b.id;
}
}
--------------------编程问答-------------------- public static bool operator !=(var a,var b)
{
//怎么判断是否为空值???
}
这个代码本身就不能编译,var不能放在函数参数中。
再说,这代码判断的是不等,不是是不是空。
如果是Nullable<T>,或者引用类型才可以判断是否为空。
方法是 if (a == default(类型)) // 空 --------------------编程问答--------------------
如果是!=null,会出现未实例化错误 --------------------编程问答--------------------
告诉你了
if (a == default(类型)) --------------------编程问答-------------------- if (a != default(类型)) --------------------编程问答-------------------- if(!(a is object))
{
} --------------------编程问答--------------------
--------------------编程问答-------------------- 什么东西啊,看不懂。 是重载吗? 重载!=? 这代码能编译吗? --------------------编程问答-------------------- 这啥写法啊,第一次看到
public static bool operator(var a,var b)
{
if(a!= 1 && b != 2){
return false;}
else {return true;}
}
补充:.NET技术 , C#