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

操作符的本质

  • 操作符的本质是函数(即算法)的“简记法”
    • 假如没有发明“+”、只有Add函数,算式3+4+5将可以写成Add(Add(3,4)5)
    • 假如没有发明“x”、只有Mul函数,那么算式3+4x5将只能写成Add(3,Mul(4,5)),注意优先级
  • 操作符不能脱离与它关联的数据类型
    • 可以说操作符就是与固定数据类型相关联的一套基本算法的简记法
    • 示例:为自定义数据类型创建操作符
using System;

namespace CreateOperator
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 6;
            int y = 4;
            int z = x / y;          //int类型的 "/" 做整型除法
            Console.WriteLine(z);   //输出1
            double a = 6;
            double b = 4;
            double c = a / b;       //double类型的 "/" 做浮点除法
            Console.WriteLine(c);   //输出1.5
        }
    }
}
using System;
using System.Collections.Generic;

namespace CreateOperator
{
    class Program
    {
        static void Main(string[] args)
        {
            Person person1 = new Person();
            Person person2 = new Person();
            person1.Name = "Adam";
            person2.Name = "Eve";
            //List<Person> nation = Person.GetMarry(person1,person2);
            List<Person> nation = person1 + person2;            //此处的"+"就是GetMarry的简记法
            foreach (var p in nation)
            {
                Console.WriteLine(p.Name);
            }
        }
    }

    class Person
    {
        public string Name;

        //public static List<Person> GetMarry(Person p1, Person p2)
        public static List<Person> operator +(Person p1, Person p2)
        {
            List<Person> people = new List<Person>();
            people.Add(p1);
            people.Add(p2);
            for (int i = 0; i < 11; i++)
            {
                Person child = new Person();
                child.Name = p1.Name + " & " + p2.Name + " 's child";
                people.Add(child);
            }
            return people;
        }
    }
}

优先级与运算顺序

  • 操作符的优先级
    • 可以使用圆括号提高被括起来表达式的优先级
    • 圆括号可以嵌套
    • 不像数学里有方括号和花括号,在C#语里 “[]” 与 “{}” 有专门的用途
  • 同优先级操作符的运算顺序
    • 除了带有赋值功能的操作符,同优先级操作符都是由左向右进行运算
    • 带有赋值功能的操作符的运算顺序是由右向左
    • 与数学运算不同,计算机语言的同优先级运算没有“结合律”
      • 3+4+5只能理解为Add(Add(3,4),5)不能理解为Add(3,Add(4,5))

各类操作符的实例

操作符

  • 成员访问操作符 x.y
using System.Windows.Forms;

namespace OperatorPriority
{
    class Program
    {
        static void Main(string[] args)
        {
            System.IO.File.Create("C:\\Users\\DELL\\Desktop\\C#\\HelloWorld.txt");
            //System.IO访问外层名称空间中的子集名称空间
            //IO.File访问名称空间中的类型
            //File.Create访问类型的静态成员
            Form myForm = new Form();
            myForm.Text = "Hello World!";
            //访问了myForm的Text属性,并把值设为了"Hello World!"
            myForm.ShowDialog();
        }
    }
}
  • 方法调用操作符 f(x)
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            double x = c.Add(3.3, 4.4);
            Console.WriteLine(x);
            c.PrintHello();         //()不能省略,调用方法
        }
    }
    class Calculator
    {
        public double Add(double a,double b)
        {
            return a + b;
        }
        public void PrintHello()
        {
            Console.WriteLine("Hello!");
        }
    }
}
  • 元素访问操作符 a[x]
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //int[] myIntArray = new int[10];
            //这个数组中有10个元素,并把10个元素初始化成这种数据类型的默认值(整数的默认值为0)
            int[] myIntArray = new int[5] {1,2,3,4,5};
            //{}为初始化器
            Console.WriteLine(myIntArray[0]);   //数组第一个数据
            Console.WriteLine(myIntArray[myIntArray.Length-1]); //数组最后一个数据
        }
    }
}
using System;
using System.Collections.Generic;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //字典
            Dictionary<string, Student> stuDic = new Dictionary<string, Student>();
            //用人名string当做索引类型,用Student当做值的类型
            for (int i = 1; i < 100; i++)
            {
                Student stu = new Student();
                stu.Name = "s_" + i.ToString();
                stu.Score = 100+i;
                stuDic.Add(stu.Name, stu);
            }
            Student number6 = stuDic["s_6"];    //[]中放的是索引,且不一定是整数
            Console.WriteLine(number6.Score);
        }
    }
    class Student
    {
        public string Name;
        public int Score;
    }
}
  • 后置的自增与自减 x++,x- -
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 100;
            int y = x++;
            /*等同于   int y = x
                       x= x + 1;   */
            Console.WriteLine(x);   //输出为101
            Console.WriteLine(y);   //输出为100
            //x++或x--遇到赋值时,先把值赋给等号左边的变量,再执行自加或自减
        }
    }
}
  • typeof操作符
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //Metadata 元数据,包含数据类型的基本信息
            Type t = typeof(int);
            Console.WriteLine(t.Namespace);
            Console.WriteLine(t.FullName);
            Console.WriteLine(t.Name);
            int c = t.GetMethods().Length;
            foreach (var mi in t.GetMethods())
            {
                Console.WriteLine(mi.Name);
            }
            Console.WriteLine(c);
        }
    }
}
  • default操作符
using System;
using System.Windows.Forms;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = default(int);   //default发现操作的类型是结构体类型,会返回内存块为0的值
            Console.WriteLine(x);   //输出为0
            Form myForm = default(Form);    //引用类型,返回空值null
            Console.WriteLine(myForm==null);    //输出为True
        }
    }
}
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Level lv = default(Level);
            Console.WriteLine(lv);
        }
    }
    enum Level
        //枚举类型声明时,编译器会把它和整数值对应起来。第一个为0,往后一次+1.
    {
        Mid=1,
        Low=0,
        High=2
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值