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

C#语言中的操作符

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

操作符的用法举例

  1. 成员访问运算符------".":用于访问类中的成员或者访问位于某个名空间中的类,如:

    cs 复制代码
    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有两种用法,一种是作为操作符,用于创建内存实例,另外一种是作为关键字用在类成员前面表示子类成员覆盖父类成员,例如:

    cs 复制代码
    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可以根据变量实例自动推断变量的类型,如:

    cs 复制代码
    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);
            }
            
        }
       
    }

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

cs 复制代码
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:该运算符用于获取一个类型的默认值,比如:

cs 复制代码
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不检查溢出,例如:

cs 复制代码
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);
            }
        }
        
    }
   
}

运行结果:

  1. (T)x,类型转换操作符:用于进行强制类型转换,该操作符只适用于两种差别不是很大的类型之间的转换,如几种数值类型之间的转换,long转换为int等,但是若是将字符串转换成数值类型的话就不可以使用该操作符进行转换,类型转换的内容比较多,放在文章最后了

  2. is:该操作符用于判断某一个对象是否属于某一个类,所以判断的要求判断的变量是一个类类型的变量;

  3. as:该操作符的作用是用于判断某一个变量是否属于某一个类,如果是的话返回该变量的首地址否则返回null,例如:

    cs 复制代码
    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);
                  
            }
            
        }
       
    }

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

cs 复制代码
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分支的功能,如:

```cs
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);
            
              
        }
        
    }
   
}
```

![](https://file.jishuzhan.net/article/1684947861222985729/a775500edf8a4f93b3fd80204e174d31.png)

C#的类型转换

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

cs 复制代码
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方法进行转换,注意该方法只能用于将格式正确的数值类型的字符串转换成目标数值类型,使用示例:

cs 复制代码
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);   
        }
        
    }
   
}
相关推荐
ling1s1 小时前
C#基础(4)封装——成员方法
开发语言·c#
爱数学的程序猿2 小时前
【C++篇】启航——初识C++(上篇)
开发语言·c++
一丝晨光2 小时前
void类型
java·开发语言·javascript·c++·c#·go·c
雷工笔记4 小时前
C#知识|设计模式的分类及认识
开发语言·设计模式·c#
林小果14 小时前
访问者模式
java·开发语言·设计模式·访问者模式
学步_技术4 小时前
Python编码系列—Python访问者模式:为对象结构添加新功能的艺术
开发语言·python·访问者模式
六点半8885 小时前
【C++】“list”的介绍和常用接口的模拟实现
开发语言·数据结构·c++·算法·青少年编程·list
鸽芷咕5 小时前
【Python报错已解决】 Encountered error while trying to install package.> lxml
开发语言·python·bug
九离十5 小时前
初识C语言(五)
c语言·开发语言
坊钰5 小时前
【Java SE 题库】移除元素(暴力解法)--力扣
java·开发语言·学习·算法·leetcode