刘铁猛C#教程笔记——操作符

C#语言中的操作符

 表中位于同一行的操作符优先级相同,从上到下优先级依次减弱;

操作符的用法举例

  1. 成员访问运算符——“.”:用于访问类中的成员或者访问位于某个名空间中的类,如:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace course
    {
        class Program
        {
            static void Main(string[] args)
            {
                Example e;
                e = new Example();
                double result = e.GetCone(3D, 4D);         //访问类中的函数成员
                Console.WriteLine(result);
    
                //引用命名空间中的某个类
    
                System.Windows.Forms.Form f = new System.Windows.Forms.Form();    //创建窗体类对象
                f.ShowDialog();            //用于显示窗体
    
            }
            
        }
        class Example
        {
            public double GetCircleArea(double r)
            {
                return Math.PI * r * r;
            }
    
            public double GetCylinder(double r,double h)
            {
                return GetCircleArea(r) * h;
            }
    
            public double GetCone(double r,double h)
            {
                return GetCylinder(r, h) / 3;
            }
        }   
    }
    

    System.Windows.Forms.Form f = new System.Windows.Forms.Form();意思是引用位于System这个命名空间中的Windows命名空间下的Forms命名空间中的Form类;命名空间是可以嵌套的。

  2. new操作符:new有两种用法,一种是作为操作符,用于创建内存实例,另外一种是作为关键字用在类成员前面表示子类成员覆盖父类成员,例如:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace course
    {
        class Program
        {
            static void Main(string[] args)
            {
                Example e=new Example();
                SubExample se =new SubExample();
    
                e.PrintHello();
                se.PrintHello();
            }
            
        }
        class Example
        {
            public void PrintHello()
            {
                Console.WriteLine("Example Say Hello");
            }
            
    
            public double GetCircleArea(double r)
            {
                return Math.PI * r * r;
            }
    
            public double GetCylinder(double r,double h)
            {
                return GetCircleArea(r) * h;
            }
    
            public double GetCone(double r,double h)
            {
                return GetCylinder(r, h) / 3;
            }
        }  
        class SubExample:Example
        {
            new public void PrintHello()
            {
                Console.WriteLine("SubExample Say Hello");
            }
    
        }
    }
    

    运行结果:

    var关键字和new操作符连用,创建匿名对象,var可以根据变量实例自动推断变量的类型,如:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace course
    {
        class Program
        {
            static void Main(string[] args)
            {
                var n = new { name = "myname", age = 19 };
                Console.WriteLine(n.GetType().Name);
            }
            
        }
       
    }
    

  3. typeof操作符:可以用于获取一个类型的完整信息,包括完整的类型名称,该类型包含在哪个命名空间中,该类型包含哪些成员等等,例如:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace course
    {
        class Program
        {
            static void Main(string[] args)
            {
                Type t = typeof(int);
                Console.WriteLine(t.Name);           //可以获取该类型的所有信息,包括该类型的全称,位于哪一个命名空间,有哪些方法,有哪些成员等等
    
                //获取该类型所拥有的成员方法
                foreach (var m in t.GetMethods())
                {
                    Console.WriteLine(m.Name);
                }
            }
            
        }
       
    }
    

     

  4. default:该运算符用于获取一个类型的默认值,比如:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace course
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(default(double));
            }
            
        }
       
    }
    

     

  5. checked/unchecked:checked检查程序是否存在溢出,unchecked不检查溢出,例如:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace course
    {
        class Program
        {
            static void Main(string[] args)
            {
                int a = int.MaxValue;
                Console.WriteLine(a);
    
                checked
                {
    
                    int b = a + 1;
                    Console.WriteLine(b);
    
                    int c = a + 1;
                    Console.WriteLine(c);
                }
            }
            
        }
       
    }
    
     运行结果:

  6.  (T)x,类型转换操作符:用于进行强制类型转换,该操作符只适用于两种差别不是很大的类型之间的转换,如几种数值类型之间的转换,long转换为int等,但是若是将字符串转换成数值类型的话就不可以使用该操作符进行转换,类型转换的内容比较多,放在文章最后了
  7. is:该操作符用于判断某一个对象是否属于某一个类,所以判断的要求判断的变量是一个类类型的变量;
  8. as:该操作符的作用是用于判断某一个变量是否属于某一个类,如果是的话返回该变量的首地址否则返回null,例如:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace course
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                string a = "is_string";
                var b = a as string;
                Console.WriteLine(b);
                  
            }
            
        }
       
    }
    

  9. ??:该操作符用于判断一个值是否为空值,如果是空值则为其重新赋予一个值,例如:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace course
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Nullable<int> a = null;      //等价于int?a=null;
                int? c = null;
                var b = a ?? 2;
                Console.WriteLine(b);
                var d = c ?? 3;
                Console.WriteLine(d);
                
                  
            }
            
        }
       
    }
    

     

  10. ?:条件操作符,实现简单的if..else分支的功能,如:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace course
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Nullable<int> a = null;      //等价于int?a=null;
                int? c = null;
                var b = a ?? 2;
                Console.WriteLine(b);
                var d = c ?? 3;
                Console.WriteLine(d);
    
                int e = b > d ? 1 : 0;
                Console.WriteLine(e);
                
                  
            }
            
        }
       
    }
    

     

C#的类型转换

 使用convert进行强制类型转换示例:

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


namespace course
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "123.12";
            double a = Convert.ToDouble(str1);
            Console.WriteLine(a);


            double b = 13.14;
            string str2 = b.ToString();
            Console.WriteLine(str2);
        }
        
    }
   
}

将数值类型转换成字符串类型时,可以直接使用数值类型所具有的ToString方法,也可以借助Convert类;

使用目标数据类型所具有的Parse方法进行转换,注意该方法只能用于将格式正确的数值类型的字符串转换成目标数值类型,使用示例:

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


namespace course
{
    class Program
    {
        static void Main(string[] args)
        {
            double a = double.Parse("3.14");
            Console.WriteLine(a);   
        }
        
    }
   
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/44872.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

25.3 matlab里面的10中优化方法介绍——Nelder-Mead法(matlab程序)

1.简述 fminsearch函数用来求解多维无约束的线性优化问题 用derivative-free的方法找到多变量无约束函数的最小值 语法 x fminsearch(fun,x0) x fminsearch(fun,x0,options) [x,fval] fminsearch(...) [x,fval,exitflag] fminsearch(...) [x,fval,exitflag,output] fmins…

使用sftp

一、背景 新项目组前端部署方式是Build打包生成dist文件&#xff0c;交由后端部署。后来知道了vscode安装sftp前端可以自行部署。 二、实操 1、vscode安装sftp 2、 配置 ①F1 / ctrlshiftp ②命令行输入sftp -> 选择 sftp: Config ③配置信息介绍 {"name"…

vscode默认gbk编码格式打开

目录 1. 问题描述2. 解决方案 1. 问题描述 每次打开vscode都是utf-8格式打开文件&#xff0c;然后满屏的中文乱码&#xff0c;自己手动换成gbk编码 后中文显示正常&#xff0c;但是换多了很烦。 2. 解决方案 ctrlshiftP 点首选项&#xff1a;打开用户设置 加上这行在最后&…

SpringBoot静态资源访问及参数处理

静态资源访问&#xff1a; 资源访问&#xff1a; 1&#xff1a;Spring Boot 支持静态和模板化的欢迎页面。它首先在配置的静态内容位置中查找index.html文件。如果未找到&#xff0c;则查找index相关模板。如果找到任一&#xff0c;它将自动用作应用程序的欢迎页面。 2&…

Elasticsearch笔记

一、ElasticSearch概述 ElasticSearch&#xff08;简称ES&#xff09;是一个分布式、RESTful 风格的搜索引擎、数据分析引擎。ES底层是基于Apache Lucene搜索引擎库实现的&#xff0c;但是ES的目的是通过简单的RESTful API来隐藏Lucene的复杂性&#xff0c;从而让全文搜索变得简…

Redisson实现简单消息队列:优雅解决缓存清理冲突

在项目中&#xff0c;缓存是提高应用性能和响应速度的关键手段之一。然而&#xff0c;当多个模块在短时间内发布工单并且需要清理同一个接口的缓存时&#xff0c;容易引发缓存清理冲突&#xff0c;导致缓存失效的问题。为了解决这一难题&#xff0c;我们采用Redisson的消息队列…

【MCU学习】RTthread工程介绍

RT-Thread架构 RT-Thread诞生于2006年&#xff0c;是一款以开源、中立、社区化发展起来的物联网操作系统。 RT-Thread主要采用 C 语言编写&#xff0c;浅显易懂&#xff0c;且具有方便移植的特性&#xff08;可快速移植到多种主流 MCU 及模组芯片上&#xff09;。RT-Thread把面…

cocosCreator 之 ScrollView

版本&#xff1a;3.4.0 参考&#xff1a;ScrollView组件 简介 ScrollView组件作为滚动容器来使用&#xff0c;它的实现通过ScrollBar组件来展示内容的位置和Mask组件显示指定区域&#xff0c;来保证有限的区域内显示更多的内容。 它的构成部分&#xff1a; ScrollBar滚动条相…

03 shell 编程

变量 语言型 编译型语言 解释型语言 shell脚本语言是解释型语言shell脚本的本质&#xff1a;shell命令的有序集合 shell 编程的基本过程 基本过程分为三步&#xff1a; step1. 建立 shell 文件 包含任意多行操作系统命令或shell命令的文本文件; step2. 赋予shell文件执行…

23 自定义控件

案例&#xff1a;组合Spin Box和Horizontal Slider实现联动 新建Qt设计师界面&#xff1a; 选择Widget&#xff1a; 选择类名&#xff08;生成.h、.cpp、.ui文件&#xff09; 在smallWidget.ui中使用Spin Box和Horizontal Slider控件 可以自定义数字区间&#xff1a; 在主窗口w…

脑电信号处理与特征提取——1. 脑电、诱发电位和事件相关电位(胡理)

目录 一、 脑电、诱发电位和事件相关电位 1.1 EEG基本知识 1.2 经典的ERPs成分及研究 1.2.1 ERPs命名规则及分类 1.2.2 常见的脑电成分 1.2.3 P300及Oddball范式 1.2.4 N400成分 一、 脑电、诱发电位和事件相关电位 1.1 EEG基本知识 EEG(Electroencephalogram)&#x…

MFC第二十天 数值型关联变量 和单选按钮与复选框的开发应用

文章目录 数值型关联变量数值型关联变量的种类介绍 单选按钮与复选框单选按钮的组内选择原理解析单选按钮和复选框以及应用数值型关联变量的开发CMainDlg.cppCInputDlg.hCInputDlg.cpp 附录 数值型关联变量 数值型关联变量的种类介绍 1、 数值型关联变量&#xff1a; a)控件型…

全志F1C200S嵌入式驱动开发(解决spi加载过慢的问题)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】 之前的几个章节当中,我们陆续解决了spi-nor驱动的问题、uboot支持spi-nor的问题。按道理来说,下面要做的应该就是用uboot的loady命令把kernel、dtb、rootfs这些文件下载到ddr,然…

执行 yum install gcc 报 【-bash: $‘yum\302\240install\302\240gcc‘: 未找到命令】

执行 yum install gcc 报错 找了一圈&#xff0c;执行&#xff1a;sudo apt-get install yum 执行&#xff1a;wget http://yum.baseurl.org/download/3.2/yum-3.2.28.tar.gz 在线下载yum完成 对其进行解压&#xff1a;tar zxvf yum-3.2.28.tar.gz 解压后如下&#xff1a; 执行…

Tiny Player (js) - 轻量好用、免费开源的 web 视频播放开发组件,内置硬解、软解视频功能

一款简单好用的 JS 视频播放器&#xff0c;完美解决我遇到的移动端播放视频的需求&#xff0c;安利给各位。 关于 Tiny Player Tiny Player 是一个极简的视频播放器 JS 库&#xff0c;内置硬解、软解视频功能&#xff0c;支持原生控件样式以及自定义控件样式&#xff0c;小巧…

Android Service启动ANR原理

一、前言 在Service组件StartService()方式启动流程分析文章中&#xff0c;针对Context#startService()启动Service流程分析了源码&#xff0c;其实关于Service启动还有一个比较重要的点是Service启动的ANR&#xff0c;因为因为线上出现了上百例的"executing service &quo…

vue三级路由的写法

{path: "/trafficmanagement",component: Layout,redirect: "/trafficmanagement",alwaysShow: true,meta: {title: "通行模块",icon: "excel",},children: [{path: "carline",name: "carline",alwaysShow: true,…

OpenCV图像处理-视频分割静态背景-MOG/MOG2/GMG

视频分割背景 1.概念介绍2. 函数介绍MOG算法MOG2算法GMG算法 原视频获取链接 1.概念介绍 视频背景扣除原理&#xff1a;视频是一组连续的帧&#xff08;一幅幅图组成&#xff09;&#xff0c;帧与帧之间关系密切(GOP/group of picture)&#xff0c;在GOP中&#xff0c;背景几乎…

LeetCode116. 填充每个节点的下一个右侧节点指针

116. 填充每个节点的下一个右侧节点指针 文章目录 [116. 填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/)一、题目二、题解方法一&#xff1a;迭代方法二&#xff1a;递归 一、题目 给定一个 完美二叉树 &…

【nginx】nginx中root与alias的区别:

文章目录 root与alias主要区别在于nginx如何解释location后面的uri&#xff0c;这会使两者分别以不同的方式将请求映射到服务器文件上。 root的处理结果是&#xff1a;root路径&#xff0b;location路径 alias的处理结果是&#xff1a;使用alias路径替换location路径 alias是一…
最新文章