C# 操作符(1)

操作符

操作符 表达式 语句…皆是为方法服务

操作符造作数构成表达式 表达式后面加;构成语句 ,语句s组成方法体 构成算法逻辑


  • 概览

操作符(Operator) 也叫运算符

操作符用来操作数据 被操作符操作的数据叫操作数(Operand)

类别|运算符

—|—

基本|x.y

一元

乘法

加法

移位

关系和类型检测

逻辑”与“

逻辑”或“

逻辑”非“

。。。。

  • 本质

    • 函数的”简记法“ 简化算法
    • 操作符不能脱离与它相关联的数据类型
      • 操作符就是与固定数据类型相关联的一套基本算法的简记法
      • 为自定义数据类型创建操作符
    int x=5;
    int y=4;
    int z=x/y;
    Console.WriteLine(z);//输出1
    double a=5.0;
    double b=4.0;
    double c=x/y;
    Console.WriteLine(c);//输出1.25
    
    
  • 优先级

    • 可以通过()提高表达式优先级
    • ()可以嵌套
  • 同级运算顺序

    • 除了带有赋值功能的操作符,同优先级的操作符都是从左向右进行运算

    • 带有赋值功能的操作符运算顺序都是从右向左

      int x=100;
      int y=200;
      int z=300;
      x+=y+=z;
      Console.WriteLine(x);//600
      Console.WriteLine(y);//500
      Console.WriteLine(z);//300
      
    • 与数学运算不同,计算机语言的同优先级运算没有结合律

      • 3+4+5只能理解为Add(Add(3,4),5)
      • 不能理解为Add(3,Add(4,5))

    基本操作符


  • “.” 示例
class Program
{
public static void Main(string[] args)
{
    Calculator c=new Calculator();
    Action myAction=new Action(c.PrintHello);
    myAction();
}
}
class Calculator
{
    public double Add(double a,double b)
    {
        return a+b;
    }
    public void PrintHello()
    {
        Console.WriteLine("Hello");
    }
}
  • "[]"示例

    • 访问数组
    class Program
    {
    public static void Main(string[] args)
    {
     int[] myintArray=new int[10];
     	//int[] myintArray=new int[]{1,2,3,4,5};//{}初始化器
        Console.WriteLine(myintArray[0]);//访问数组第一个元素
        Console.WriteLine(myintArray[myintArray.length-1]);//访问数组最后一个元素
        
    }
    
    
    • 访问字典 "[]"放对集合元素的索引
    class Program
    {
    public static void Main(string[] args)
    {
        Dictionary<string,Student> stuDic=new Dictionary<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;
    }
    
    
  • ”++“

    class Program
    {
    public static void Main(string[] args)
    {
    	int x=100;
        x++;//x=x+1;
        Console.WriteLine(x);//101
        
    }
    }
    
    
  • ”–“

    class Program
    {
    public static void Main(string[] args)
    {
    	int x=100;
        x--;//x=x-1;
        Console.WriteLine(x);//99
        
    }
    }
    
    

    class Program
    {
    public 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
    }
    }
    

    class Program
    {
    public static void Main(string[] args)
    {
    	int x=100;
    	int y=x--;//相当于
        //int y=x;	x=x-1;
        
        Console.WriteLine(x);// 99   
        Console.WriteLine(y);// 100
    }
    }
    
  • ”typeof”操作符

    class Program
    {
    public static void Main(string[] args)
    {
    	//MataData
        Type t=typeof(int);
        Console.WriteLine(t.NameSpace);//System
        Console.WriteLine(t.FullName);//System.Int32
        Console.WriteLine(t.Name);//Int32
        int c=t.GetMethods().Length;
         foreach(var mi in t.GetMethods())
         {
             Console.WriteLine(mi.Name);
         }
        Console.WriteLine(c);
    }
    }
    
  • “default”

    • 结构体类型
    class Program
    {
    public static void Main(string[] args)
    {
    	int x = default(int);
        double y=default(double);
        Console.WriteLine(x);//0
        Console.WriteLine(y);//0
    }
    }
    
    • 枚举
    class Program
    {
    public static void Main(string[] args)
    {
    	Level level=default(Level);
        Console.WriteLine(level);//low
    }
    }
    enum Level
    {//默认情况
        Low,//0
        Mid,//1
        High//2
     //手动规定 
        //Low=1,
        //Mid=0, //默认值为Mid
        //High=2
    }
    
    
    //使用枚举的时候最好手动设置值为0的值,如果不设置的话 默认值只会返回0 不会返回枚举值
    
    • 引用
    class Program
    {
    public static void Main(string[] args)
    { 
        Form myForm= new Form();
     	Console.WriteLine(myForm==null);//True
    }
    }
    
  • new 操作符

    在内存中创建一个类型的实例 并且立刻调用这个类型的实例构造器

    var x 一个隐式类型的变量

    int x: 一个显式类型的变量

    • 调用实例的构造器
    class Program
    {	public static void Main(string[] args)
    	{
    	Form myForm=new Form();//new操作符还能得到实例的地址把实例的地址通过赋值符号“=”交给访问实实例的变量myForm 由此构成变量和实例间的引用关系
        myForm.Text="Hello
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值