当前位置:编程学习 > C#/ASP.NET >>

请问怎么写一个为的!=和==

 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(类型)) // 空 --------------------编程问答--------------------
引用 1 楼 hjywyj 的回复:
C# code
?



12345678

 class stu         {             public int id { get; set; }             public static bool operator !=(stu a, stu b)             {                 return a.id != b.id;  ……




如果是!=null,会出现未实例化错误 --------------------编程问答--------------------
引用 3 楼 q730333782 的回复:
引用 1 楼 hjywyj 的回复:C# code
?



12345678

 class stu         {             public int id { get; set; }             public static bool operator !=(stu a, stu b)             {        ……


告诉你了

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#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,