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

本文介绍了C#语言中的操作符,包括成员访问运算符、new操作符的两种用法、var关键字、typeof操作符、default运算符、checked/unchecked的溢出处理,以及类型转换的相关方法,如Convert、Parse等。此外,还涉及条件操作符和空值合并运算符的使用。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值