using System;namespace CreateOperator
{classProgram{staticvoidMain(string[] args){int x =6;int y =4;int z = x / y;//int类型的 "/" 做整型除法
Console.WriteLine(z);//输出1double a =6;double b =4;double c = a / b;//double类型的 "/" 做浮点除法
Console.WriteLine(c);//输出1.5}}}
using System;using System.Collections.Generic;namespace CreateOperator
{classProgram{staticvoidMain(string[] args){Person person1 =newPerson();Person person2 =newPerson();
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);}}}classPerson{publicstring Name;//public static List<Person> GetMarry(Person p1, Person p2)publicstatic List<Person>operator+(Person p1,Person p2){
List<Person> people =newList<Person>();
people.Add(p1);
people.Add(p2);for(int i =0; i <11; i++){Person child =newPerson();
child.Name = p1.Name +" & "+ p2.Name +" 's child";
people.Add(child);}return people;}}}
using System;namespace OperatorsExample
{classProgram{staticvoidMain(string[] args){Calculator c =newCalculator();double x = c.Add(3.3,4.4);
Console.WriteLine(x);
c.PrintHello();//()不能省略,调用方法}}classCalculator{publicdoubleAdd(double a,double b){return a + b;}publicvoidPrintHello(){
Console.WriteLine("Hello!");}}}
元素访问操作符 a[x]
using System;namespace OperatorsExample
{classProgram{staticvoidMain(string[] args){//int[] myIntArray = new int[10];//这个数组中有10个元素,并把10个元素初始化成这种数据类型的默认值(整数的默认值为0)int[] myIntArray =newint[5]{1,2,3,4,5};//{}为初始化器
Console.WriteLine(myIntArray[0]);//数组第一个数据
Console.WriteLine(myIntArray[myIntArray.Length-1]);//数组最后一个数据}}}
using System;using System.Collections.Generic;namespace OperatorsExample
{classProgram{staticvoidMain(string[] args){//字典
Dictionary<string, Student> stuDic =newDictionary<string,Student>();//用人名string当做索引类型,用Student当做值的类型for(int i =1; i <100; i++){Student stu =newStudent();
stu.Name ="s_"+ i.ToString();
stu.Score =100+i;
stuDic.Add(stu.Name, stu);}Student number6 = stuDic["s_6"];//[]中放的是索引,且不一定是整数
Console.WriteLine(number6.Score);}}classStudent{publicstring Name;publicint Score;}}
后置的自增与自减 x++,x- -
using System;namespace OperatorsExample
{classProgram{staticvoidMain(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
{classProgram{staticvoidMain(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
{classProgram{staticvoidMain(string[] args){int x =default(int);//default发现操作的类型是结构体类型,会返回内存块为0的值
Console.WriteLine(x);//输出为0Form myForm =default(Form);//引用类型,返回空值null
Console.WriteLine(myForm==null);//输出为True}}}
操作符的本质操作符的本质是函数(即算法)的“简记法”假如没有发明“+”、只有Add函数,算式3+4+5将可以写成Add(Add(3,4)5)假如没有发明“x”、只有Mul函数,那么算式3+4x5将只能写成Add(3,Mul(4,5)),注意优先级操作符不能脱离与它关联的数据类型可以说操作符就是与固定数据类型相关联的一套基本算法的简记法示例:为自定义数据类型创建操作符using System;namespace CreateOperator{ class Progr