C#运算符及其重载

基本的运算符和C语言基本一样。关于基础部分这里不会细说

一、比较特殊运算符

1、类型信息运算符

(1)sizeof //判断变量占用几个字节的控件

Console.WriteLine("int 类型的占用字节数为:"+sizeof(int)+"字节");

(2)is //判断对象是否为指定的类型

int x=0;
Console.WriteLine(x is int);     //返回的结果为True

2、checked和unchecked运算符
如下代码:

byte B = 255;
B++;
Console.WriteLine(B.ToString());     //输出的结果为0

如果变量存储的值溢出了,则往往我们容易忽略不被察觉。为了提高代码健壮性,我们希望能够检查溢出的问题。测试可以通过加上checked运算符来检查。

byte B = 255;
checked                //编译时会弹出错误提示。提高代码的健壮性
{
    B++;
}
Console.WriteLine(B.ToString());

unchedked就是不检查,和忽略不写效果一样

byte B = 255;
unchecked              //编译时不会弹出错误提醒,最终B的值为0
{
    B++;
}
Console.WriteLine(B.ToString());

3、as //检查引用类型的显示类型转换与指定类型是否兼容

object o1="Some String";
objrct o2=5;
string s1=o1 as string;     //s1="Some String"
string s2=o2 as string;     //s2=null

4、typeof //返回一个表示特定类型的System.Type对象

typeof(string)返回表示Sytem.String的Type对象

二、装箱拆箱
请看下面的代码:

Console.WriteLine(10.ToString());    //输出的结果为10

上述的代码会将数字10转换为字符串输出。但是10是个常量数值,它又怎么能调用ToString()方法呢?
其根本原因在于装箱、拆箱操作。具体过程如下:
1、先通过装箱boxing操作将值10转换为引用类型(即objec类型)。运行库会为堆上的对象创建一个零时的引用类型的箱子
装箱操作可以隐式进行,也可显示进行。如下:

int x=10;
object y=x;    //显示的装箱操作

2、调用object的ToString()方法
3、拆箱操作与装箱相反,它会吧引用类型再转换为值类型。删除托管堆上的零时的空间。
也可显示进行:

int X=int)y;

三、比较相等性
1、ReferenceEquals()方法
ReferenceEquals()方法是个静态方法。实例如下:

Vector  u,v,w;
u = new Vector ();
v = new Vector ();
w = u;
bool B1 = ReferenceEquals(null, null);   //返回的为True
bool B2 = ReferenceEquals(null, u);   //返回的为false
bool B3 = ReferenceEquals(u, w);   //返回的为true

2、Equals() //比较引用是否相等

Vector  u,v,w;
u = new Vector ();
v = new Vector ();
w = u;
bool B4 = w.Equals(u);             //返回的为true

三、运算符

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

namespace test
{
    class Vector
    {
        private int x;
        private int y;
        private int z;

        public Vector(int x,int y,int z)         //有参构造函数
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public Vector()                           //无参构造函数
        { 
            this.x = 0;
            this.y = 0;
            this.z = 0;
        }

        public static Vector operator + (Vector v1,Vector v2)    //运算符重载一定是public static,它是静态的不属于任何特定对象。他没有函数名,有的是operator +
        {
            Vector vSum=new Vector(0,0,0);      
            vSum.x = v1.x+v2.x;
            vSum.y = v1.y+v2.y;
            vSum.z = v1.z+v2.z;
            return vSum;  
        }

        public static Vector operator +(Vector v1, int i)     //只能匹配Vector+i,这个还看数值和对象的顺序
        {
            return new Vector(v1.x + i, v1.y + i, v1.z + i);
        }

        public static Vector operator +(int i, Vector v1)     //只能匹配i+Vector
        {
            return new Vector(v1.x + i, v1.y + i, v1.z + i);
        }

        //可将上述代码写成下面的样子。
        //public static Vector operator +(int i, Vector v1)
        //{
        //    return v1 + i;       //重用上述代码  public static Vector operator +(Vector v1, int i)  
        //}

        public static bool operator ==(Vector v1, Vector v2)   //重载运算符==,必须也要重载!=否则会报错
        {
            if (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z)
                return true;
            else
                return false;
        }

        public static bool operator !=(Vector v1, Vector v2)   //重载运算符!=,必须也要重载==否则会报错
        {
            return !(v1 == v2);
        }

        public override string ToString()     //重写ToString()方法,输出类中数据的值,使用默认的值,返回的为引用
        {
            return "(" + this.x +","+ this.y +","+ this.z + ")";
        }


    }
    class Program
    {       
        static void Main(string[] args)
        {
            Vector a = new Vector(3,3,1);
            Vector b = new Vector(2,-4,-4);
            Console.WriteLine("a==b is "+(a==b));
            Console.WriteLine("a!=b is "+(a!=b));
            Vector c = a + b;
            Console.WriteLine("a=" + a.ToString());
            Console.WriteLine("b=" + b.ToString());
            Console.WriteLine("c=" + c.ToString());

            Vector d = 2+c;        //不能调换顺序,只能写成c+2,此时调用的是int Vector顺序的操作符重载函数
            Console.WriteLine("d="+d.ToString());
            d+=2;                  //+=并不是重载,而是自动按照d=d+2;来处理的
            Console.ReadKey();
        }
    }
}

在这里插入图片描述

有些操作符重载必须成对进行。否则会报错,如下:
// ==和!=
// >和<
// >=和<=

发布了51 篇原创文章 · 获赞 0 · 访问量 879

猜你喜欢

转载自blog.csdn.net/weixin_40786497/article/details/103845949