C#学习笔记 操作符(下)

各类操作符的示例

操作符

  • 类型转换
  • 隐式(implicit)类型转换
    • 不丢失精度的转换
    • 子类向父类的转换
    • 装箱
  • 显式(explicit)类型转换
    • 有可能丢失精度(甚至发生错误)的转换,即cast
    • 拆箱
    • 使用Convert类
    • ToString方法与各数据类型的Parse/TryParse方法
  • 自定义类型转换操作符
    • 示例
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = Console.ReadLine();
            string str2 = Console.ReadLine();
            //Console.WriteLine(str1 + str2);       //输出结果为两个字符串相接
            int x = Convert.ToInt32(str1);          //类型转换
            int y = Convert.ToInt32(str2);
            Console.WriteLine(x + y);               //输出结果为两个整数相加
        }
    }
}
  • 不丢失精度的转换
    隐式数值转换为:
    • 从 sbyte 到 short、int、long、float、double 或 decimal。
    • 从 byte 到 short、ushort、int、uint、long、ulong、float、double 或 decimal。
    • 从 short 到 int、long、float、double 或 decimal。
    • 从 ushort 到 int、uint、long、ulong、float、double 或 decimal。
    • 从 int 到 long、float、double 或 decimal。
    • 从 uint 到 long、ulong、float、double 或 decimal。
    • 从 long 到 float、double 或 decimal。
    • 从 ulong 到 float、double 或 decimal。
    • 从 char 到 ushort、int、uint、long、ulong、float、double 或 decimal。
    • 从 float 到 double。
    从 int、uint、long 或 ulong 到 float 的转换以及从 long 或 ulong 到 double 的转换可能导致精度损失,但决不会影响数值大小。其他的隐式数值转换决不会丢失任何信息。
    不存在向 char 类型的隐式转换,因此其他整型的值不会自动转换为 char 类型。
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = int.MaxValue;
            long y = x;     //隐式类型转换(不丢失精度的前提下)
            Console.WriteLine(y);
        }
    }
}
  • 子类向父类的转换
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Teacher t = new Teacher();
            Human h = t;    //此处包含了子类向父类的隐式类型转换
            Animal a = h;
            a.Eat();    //只能访问Eat方法
        }
    }
    class Animal
    {
        public void Eat()
        {
            Console.WriteLine("Eating...");
        }
    }
    class Human : Animal        //继承
    {
        public void Think()
        {
            Console.WriteLine("Who I am?");
        }
    }
    class Teacher : Human       //继承
    {
        public void Teach()
        {
            Console.WriteLine("I teach programing.");
        }
    }
}
  • 显式类型转换
    显式数值转换是指从一个 numeric-type 到另一个 numeric-type 的转换,此转换不能用已知的隐式数值转换(第 ‎6.1.2 节)实现,它包括:
    • 从 sbyte 到 byte、ushort、uint、ulong 或 char。
    • 从 byte 到 sbyte 和 char。
    • 从 short 到 sbyte、byte、ushort、uint、ulong 或 char。
    • 从 ushort 到 sbyte、byte、short 或 char。
    • 从 int 到 sbyte、byte、short、ushort、uint、ulong 或 char。
    • 从 uint 到 sbyte、byte、short、ushort、int 或 char。
    • 从 long 到 sbyte、byte、short、ushort、int、uint、ulong 或 char。
    • 从 ulong 到 sbyte、byte、short、ushort、int、uint、long 或 char。
    • 从 char 到 sbyte、byte 或 short。
    • 从 float 到 sbyte、byte、short、ushort、int、uint、long、ulong、char 或 decimal。
    • 从 double 到 sbyte、byte、short、ushort、int、uint、long、ulong、char、float 或 decimal。
    • 从 decimal 到 sbyte、byte、short、ushort、int、uint、long、ulong、char、float 或 double。
    由于显式转换包括所有隐式和显式数值转换,因此总是可以使用强制转换表达式(第 ‎7.7.6 节)从任何 nnumeric-type 转换为任何其他的 numeric-type。
  • (T)x 类型转换,即cast
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(ushort.MaxValue);
            uint x = 65536;
            ushort y = (ushort)x;   //将32位的数据强行塞进16位的空间
            Console.WriteLine(y);   //输出为0
        }
    }
}
  • Convert类
    相当于一个类型转换的枢纽,几乎可以把任何一个数据类型转化为我们想要的数据类型。
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = "100";
            //int i = (int)x;       //无法编译
            string y = "200";
            int i = Convert.ToInt32(y);				//字符串转化为整数
            Console.WriteLine(i.GetType().Name);    //输出为Int32
        }
    }
}
  • 自定义类型转换操作符
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Stone st = new Stone();
            st.Age = 5000;
            Monkey wukongSun = (Monkey)st;
            //Monkey wukongSun = st;            隐式转换
            Console.WriteLine(wukongSun.Age);
        }
    }
    class Stone
    {
        public int Age;
        public static explicit operator Monkey(Stone st)
        //显示类型转换操作符,可以理解为目标类型的构造器,但是写在目标类型里,而是写在被转换的类型里。
        //public static implicit operator Monkey(Stone st)          隐式转换
        {
            Monkey m = new Monkey();
            m.Age = st.Age / 500;
            return m;
        }
    }
    class Monkey
    {
        public int Age;
    }
}

  • 位移操作符 << ,>>
    指数据在内存当中的二进制的结构,向左或向右进行一定位数的平移。
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 7;
            int y = x << 1;         //向左平移一位
            string strX = Convert.ToString(x, 2).PadLeft(32, '0');
            string strY = Convert.ToString(y, 2).PadLeft(32, '0');
            Console.WriteLine(strX);
            Console.WriteLine(strY);
        }
    }
}
  • 按位求与 &
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 7;
            int y = 21;
            int z = x & y;
            string strX = Convert.ToString(x, 2).PadLeft(32, '0');
            string strY = Convert.ToString(y, 2).PadLeft(32, '0');
            string strZ = Convert.ToString(z, 2).PadLeft(32, '0');
            Console.WriteLine(strX);
            Console.WriteLine(strY);
            Console.WriteLine(strZ);
        }
    }
}

按位求与

  • 按位求或 |
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 7;
            int y = 21;
            int z = x | y;
            string strX = Convert.ToString(x, 2).PadLeft(32, '0');
            string strY = Convert.ToString(y, 2).PadLeft(32, '0');
            string strZ = Convert.ToString(z, 2).PadLeft(32, '0');
            Console.WriteLine(strX);
            Console.WriteLine(strY);
            Console.WriteLine(strZ);
        }
    }
}

按位求或

  • 按位异或 ^
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 7;
            int y = 21;
            int z = x ^ y;
            string strX = Convert.ToString(x, 2).PadLeft(32, '0');
            string strY = Convert.ToString(y, 2).PadLeft(32, '0');
            string strZ = Convert.ToString(z, 2).PadLeft(32, '0');
            Console.WriteLine(strX);
            Console.WriteLine(strY);
            Console.WriteLine(strZ);
        }
    }
}

按位异或

  • 条件与 AND &&
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 5;
            int y = 4;
            int a = 100;
            int b = 200;
            if (x>y && a < b)   //a并且b,两边同时满足为True
            {
                Console.WriteLine("Hello");
            }
        }
    }
}
  • 条件或 OR ||
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 3;
            int y = 4;
            int a = 100;
            int b = 200;
            if (x>y || a < b)   //a或b,任意一边满足为True
            {
                Console.WriteLine("Hello");
            }
        }
    }
}
  • null合并
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //Nullable<int> x = null;     //可空类型
            int? x = null;
            int y = x ?? 1;             //如果x为null,则把1赋值给x
            Console.WriteLine(y);		//输出为1
        }
    }
}
  • 条件 ?:
    本质上是if else分支的简写
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 80;
            string str = (x >= 60) ? "Pass" : "Failed";
            //如果为真,返回:左边的值;为假,则返回:右边的值
            /*string str = string.Empty;
            if (x>=60)
            {
                str = "Pass";
            }
            else
            {
                str = "Failed";
            }*/
            Console.WriteLine(str);
        }
    }
}
  • 赋值操作符
using System;
namespace ConversionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 5;
            int y = 6;
            int z = 7;
            int a = x += y *= z;    //从右向左运算
            Console.WriteLine(y);
            Console.WriteLine(x);
            Console.WriteLine(a);   //输出47
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值